303 template <typename T> |
303 template <typename T> |
304 class Array: public MetaspaceObj { |
304 class Array: public MetaspaceObj { |
305 friend class MetadataFactory; |
305 friend class MetadataFactory; |
306 friend class VMStructs; |
306 friend class VMStructs; |
307 friend class MethodHandleCompiler; // special case |
307 friend class MethodHandleCompiler; // special case |
|
308 friend class WhiteBox; |
308 protected: |
309 protected: |
309 int _length; // the number of array elements |
310 int _length; // the number of array elements |
310 T _data[1]; // the array memory |
311 T _data[1]; // the array memory |
311 |
312 |
312 void initialize(int length) { |
313 void initialize(int length) { |
323 return (void*) Metaspace::allocate(loader_data, word_size, read_only, |
324 return (void*) Metaspace::allocate(loader_data, word_size, read_only, |
324 MetaspaceObj::array_type(sizeof(T)), CHECK_NULL); |
325 MetaspaceObj::array_type(sizeof(T)), CHECK_NULL); |
325 } |
326 } |
326 |
327 |
327 static size_t byte_sizeof(int length) { return sizeof(Array<T>) + MAX2(length - 1, 0) * sizeof(T); } |
328 static size_t byte_sizeof(int length) { return sizeof(Array<T>) + MAX2(length - 1, 0) * sizeof(T); } |
|
329 |
|
330 // WhiteBox API helper. |
|
331 static int bytes_to_length(size_t bytes) { |
|
332 assert(is_size_aligned(bytes, BytesPerWord), "Must be, for now"); |
|
333 |
|
334 if (sizeof(Array<T>) >= bytes) { |
|
335 return 0; |
|
336 } |
|
337 |
|
338 size_t left = bytes - sizeof(Array<T>); |
|
339 assert(is_size_aligned(left, sizeof(T)), "Must be"); |
|
340 |
|
341 size_t elements = left / sizeof(T); |
|
342 assert(elements <= (size_t)INT_MAX, err_msg("number of elements " SIZE_FORMAT "doesn't fit into an int.", elements)); |
|
343 |
|
344 int length = (int)elements; |
|
345 |
|
346 assert((size_t)size(length) * BytesPerWord == bytes, |
|
347 err_msg("Expected: " SIZE_FORMAT " got: " SIZE_FORMAT, |
|
348 bytes, (size_t)size(length) * BytesPerWord)); |
|
349 |
|
350 return length; |
|
351 } |
328 |
352 |
329 explicit Array(int length) : _length(length) { |
353 explicit Array(int length) : _length(length) { |
330 assert(length >= 0, "illegal length"); |
354 assert(length >= 0, "illegal length"); |
331 } |
355 } |
332 |
356 |