Realm
A distributed, event-based tasking library
Loading...
Searching...
No Matches
bytearray.h
Go to the documentation of this file.
1/*
2 * Copyright 2025 Stanford University, NVIDIA Corporation
3 * SPDX-License-Identifier: Apache-2.0
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18// a little helper class for storing a dynamically allocated byte array
19// an accessing it in various ways
20
21#ifndef REALM_BYTEARRAY_H
22#define REALM_BYTEARRAY_H
23
24#include <stddef.h>
25
26namespace Realm {
27
28 // a ByteArrayRef is a const reference to somebody else's storage (e.g. a
29 // ByteArray)
31 public:
33 ByteArrayRef(const void *ref_base, size_t ref_size);
35
36 // change what this ByteArrayRef refers to
37 ByteArrayRef &changeref(const void *ref_base, size_t ref_size);
38
39 // access to base pointer and size
40 const void *base(void) const;
41
42 size_t size(void) const;
43
44 // helper to access bytes as typed references
45 template <typename T>
46 const T &at(size_t offset) const;
47
48 protected:
50 size_t array_size;
51 };
52
53 class ByteArray : public ByteArrayRef {
54 public:
55 ByteArray(void);
56 ByteArray(const void *copy_from, size_t copy_size);
57 ByteArray(const ByteArray &copy_from);
58
59 // not actually a copy constructor! blech...
60 ByteArray(const ByteArrayRef &copy_from);
61
63
64 // copies the contents of the rhs ByteArray (again two versions)
65 ByteArray &operator=(const ByteArrayRef &copy_from);
66 ByteArray &operator=(const ByteArray &copy_from);
67
68 // swaps the contents of two ByteArrays - returns a reference to the first one
69 // this allows you to transfer ownership of a byte array to a called function via:
70 // ByteArray().swap(old_array)
71 ByteArray &swap(ByteArray &swap_with);
72
73 // copy raw data in
74 ByteArray &set(const void *copy_from, size_t copy_size);
75
76 // access to base pointer and size
77 // (const versions are redeclared due to some C++ weirdness)
78 void *base(void);
79 const void *base(void) const;
80
81 // helper to access bytes as typed references
82 template <typename T>
83 T &at(size_t offset);
84
85 template <typename T>
86 const T &at(size_t offset) const;
87
88 // give ownership of a buffer to a ByteArray
89 ByteArray &attach(void *new_base, size_t new_size);
90
91 // explicitly deallocate any held storage
92 void clear(void);
93
94 // extract the pointer from the ByteArray (caller assumes ownership)
95 void *detach(void);
96
97 protected:
98 void make_copy(const void *copy_base, size_t copy_size);
99 };
100
101 // support for realm-style serialization
102 template <typename S>
103 bool serialize(S &serdez, const ByteArrayRef &a);
104
105 template <typename S>
106 bool serialize(S &serdez, const ByteArray &a);
107
108 template <typename S>
109 bool deserialize(S &serdez, ByteArray &a);
110
111}; // namespace Realm
112
113#include "realm/bytearray.inl"
114
115#endif
Definition bytearray.h:30
size_t array_size
Definition bytearray.h:50
size_t size(void) const
ByteArrayRef(const ByteArrayRef &ref)
const void * base(void) const
void * array_base
Definition bytearray.h:49
ByteArrayRef(const void *ref_base, size_t ref_size)
ByteArrayRef & changeref(const void *ref_base, size_t ref_size)
const T & at(size_t offset) const
Definition bytearray.h:53
ByteArray & operator=(const ByteArray &copy_from)
ByteArray & operator=(const ByteArrayRef &copy_from)
ByteArray(const void *copy_from, size_t copy_size)
ByteArray(const ByteArray &copy_from)
ByteArray(const ByteArrayRef &copy_from)
ByteArray & attach(void *new_base, size_t new_size)
ByteArray & swap(ByteArray &swap_with)
void * detach(void)
const T & at(size_t offset) const
ByteArray & set(const void *copy_from, size_t copy_size)
void make_copy(const void *copy_base, size_t copy_size)
void * base(void)
T & at(size_t offset)
void clear(void)
const void * base(void) const
Definition activemsg.h:38
bool serialize(S &serdez, const ByteArrayRef &a)
bool deserialize(S &serdez, ByteArray &a)