- ORBMM();
-
- void *alloc(size_t size, int refs = 1);
- void retain(void *ptr, int refs = 1);
- void release(void *ptr, int refs = 1);
- void add_region(Region region, bool unmap_orig, int refs = 1);
- };
-
- extern ORBMM *orbmm;
-
- // FIXME: should be an IDL exception
- struct ArrayException
- {
- };
-
- class NullArray {
- };
-
- static const NullArray nullarray = {};
-
- template<typename T> struct MutableArray {
- T *ptr;
- size_t count;
-
- bool valid_index(size_t index)
- {
- return index >= 0 && index < count;
- }
-
- T &operator[](size_t index)
- {
-#ifndef POLINTOS_NO_ARRAY_BOUNDS_CHECK
- if (!valid_index(index))
- throw ArrayException();
-#endif
-
- return ptr[index];
- }
-
- MutableArray()
- {
- ptr = NULL;
- count = 0;
- }
-
- MutableArray(NullArray na)
- {
- ptr = NULL;
- count = 0;
- }
-
- MutableArray(T *PTR, size_t COUNT)
- {
- ptr = PTR;
- count = COUNT;
- }
-
- MutableArray &slice_nocheck(size_t first, size_t newcount)
- {
- MutableArray ret;
- ret.ptr = ptr + first;
- ret.count = newcount;
- return ret;
- }
-
- MutableArray &slice_nocheck(size_t first)
- {
- MutableArray ret;
- ret.ptr = ptr + first;
- ret.count = count - first;
- return ret;
- }
-
- MutableArray &slice(size_t first, size_t count)
- {
-#ifndef POLINTOS_NO_ARRAY_BOUNDS_CHECK
- if (!valid_index(first) || !valid_index(first + count - 1))
- throw ArrayException();
-#endif
-
- return slice_nocheck(first, count);
- }
-
- MutableArray &slice(size_t first)
- {
-#ifndef POLINTOS_NO_ARRAY_BOUNDS_CHECK
- if (!valid_index(first))
- throw ArrayException();
-#endif
-
- return slice_nocheck(first);
- }
-
- MutableArray copy()
- {
- MutableArray new_arr;
- new_arr.ptr = new(orbmm) T[count];
- new_arr.count = count;
- memcpy(new_arr.ptr, ptr, count);
- return new_arr;
- }
- };
-
- template<typename T>
- struct GrowableArray : public MutableArray<T> {
- using MutableArray<T>::ptr;
- using MutableArray<T>::count;
- size_t bufsize;
-
- GrowableArray()
- {
- bufsize = 0;
- }
-
- GrowableArray(NullArray na) : MutableArray<T>(na)
- {
- bufsize = 0;
- }
-
- GrowableArray(T *PTR, size_t COUNT) : MutableArray<T>(PTR, COUNT)
- {
- bufsize = COUNT;
- }
-
- GrowableArray(T *PTR, size_t COUNT, size_t BUFSIZE) :
- MutableArray<T>(PTR, COUNT)
- {
- bufsize = BUFSIZE;
- }
-
- GrowableArray(MutableArray<T> &ma) : MutableArray<T>(ma)
- {
- bufsize = count;
- }
-
- void grow(size_t newsize)
- {
- if (newsize <= bufsize)
- return;
-
- T *oldptr = ptr;
- T *newptr = new(orbmm) T[newsize];
- memcpy(newptr, ptr, count * sizeof(T));
- ptr = newptr;
- ll_smp_membar_store_after_store();
- bufsize = newsize;
- // FIXME: Should release imply membar?
- ll_smp_membar_any_after_store();
- orbmm->release(oldptr);
- }
-
- // Caller must sync against all writers.
- void append(T *newptr, size_t len, size_t max = ULONG_MAX)
- {
- if (count + len < count)
- throw ArrayException();
-
- if (count + len > max)
- throw ArrayException();
-
- if (count + len > bufsize)
- grow(ll_get_order_round_up(count + len));
-
- memcpy(ptr + count, newptr, len * sizeof(T));
- count += len;
- }