62 |
62 |
63 // WhiteBox API helper. |
63 // WhiteBox API helper. |
64 // Can't distinguish between array of length 0 and length 1, |
64 // Can't distinguish between array of length 0 and length 1, |
65 // will always return 0 in those cases. |
65 // will always return 0 in those cases. |
66 static int bytes_to_length(size_t bytes) { |
66 static int bytes_to_length(size_t bytes) { |
67 assert(is_size_aligned(bytes, BytesPerWord), "Must be, for now"); |
67 assert(is_aligned(bytes, BytesPerWord), "Must be, for now"); |
68 |
68 |
69 if (sizeof(Array<T>) >= bytes) { |
69 if (sizeof(Array<T>) >= bytes) { |
70 return 0; |
70 return 0; |
71 } |
71 } |
72 |
72 |
73 size_t left = bytes - sizeof(Array<T>); |
73 size_t left = bytes - sizeof(Array<T>); |
74 assert(is_size_aligned(left, sizeof(T)), "Must be"); |
74 assert(is_aligned(left, sizeof(T)), "Must be"); |
75 |
75 |
76 size_t elements = left / sizeof(T); |
76 size_t elements = left / sizeof(T); |
77 assert(elements <= (size_t)INT_MAX, "number of elements " SIZE_FORMAT "doesn't fit into an int.", elements); |
77 assert(elements <= (size_t)INT_MAX, "number of elements " SIZE_FORMAT "doesn't fit into an int.", elements); |
78 |
78 |
79 int length = (int)elements; |
79 int length = (int)elements; |
120 |
120 |
121 T at_acquire(const int which) { return OrderAccess::load_acquire(adr_at(which)); } |
121 T at_acquire(const int which) { return OrderAccess::load_acquire(adr_at(which)); } |
122 void release_at_put(int which, T contents) { OrderAccess::release_store(adr_at(which), contents); } |
122 void release_at_put(int which, T contents) { OrderAccess::release_store(adr_at(which), contents); } |
123 |
123 |
124 static int size(int length) { |
124 static int size(int length) { |
125 size_t bytes = align_size_up(byte_sizeof(length), BytesPerWord); |
125 size_t bytes = align_up(byte_sizeof(length), BytesPerWord); |
126 size_t words = bytes / BytesPerWord; |
126 size_t words = bytes / BytesPerWord; |
127 |
127 |
128 assert(words <= INT_MAX, "Overflow: " SIZE_FORMAT, words); |
128 assert(words <= INT_MAX, "Overflow: " SIZE_FORMAT, words); |
129 |
129 |
130 return (int)words; |
130 return (int)words; |