324 return (void*) Metaspace::allocate(loader_data, word_size, read_only, 325 MetaspaceObj::array_type(sizeof(T)), THREAD); 326 } 327 328 static size_t byte_sizeof(int length) { return sizeof(Array<T>) + MAX2(length - 1, 0) * sizeof(T); } 329 330 // WhiteBox API helper. 331 // Can't distinguish between array of length 0 and length 1, 332 // will always return 0 in those cases. 333 static int bytes_to_length(size_t bytes) { 334 assert(is_size_aligned(bytes, BytesPerWord), "Must be, for now"); 335 336 if (sizeof(Array<T>) >= bytes) { 337 return 0; 338 } 339 340 size_t left = bytes - sizeof(Array<T>); 341 assert(is_size_aligned(left, sizeof(T)), "Must be"); 342 343 size_t elements = left / sizeof(T); 344 assert(elements <= (size_t)INT_MAX, err_msg("number of elements " SIZE_FORMAT "doesn't fit into an int.", elements)); 345 346 int length = (int)elements; 347 348 assert((size_t)size(length) * BytesPerWord == bytes, 349 err_msg("Expected: " SIZE_FORMAT " got: " SIZE_FORMAT, 350 bytes, (size_t)size(length) * BytesPerWord)); 351 352 return length; 353 } 354 355 explicit Array(int length) : _length(length) { 356 assert(length >= 0, "illegal length"); 357 } 358 359 Array(int length, T init) : _length(length) { 360 assert(length >= 0, "illegal length"); 361 for (int i = 0; i < length; i++) { 362 _data[i] = init; 363 } 364 } 365 366 public: 367 368 // standard operations 369 int length() const { return _length; } 370 T* data() { return _data; } 371 bool is_empty() const { return length() == 0; } 372 373 int index_of(const T& x) const { 374 int i = length(); 375 while (i-- > 0 && _data[i] != x) ; 376 377 return i; 378 } 379 380 // sort the array. 381 bool contains(const T& x) const { return index_of(x) >= 0; } 382 383 T at(int i) const { assert(i >= 0 && i< _length, err_msg("oob: 0 <= %d < %d", i, _length)); return _data[i]; } 384 void at_put(const int i, const T& x) { assert(i >= 0 && i< _length, err_msg("oob: 0 <= %d < %d", i, _length)); _data[i] = x; } 385 T* adr_at(const int i) { assert(i >= 0 && i< _length, err_msg("oob: 0 <= %d < %d", i, _length)); return &_data[i]; } 386 int find(const T& x) { return index_of(x); } 387 388 T at_acquire(const int which) { return OrderAccess::load_acquire(adr_at(which)); } 389 void release_at_put(int which, T contents) { OrderAccess::release_store(adr_at(which), contents); } 390 391 static int size(int length) { 392 return align_size_up(byte_sizeof(length), BytesPerWord) / BytesPerWord; 393 } 394 395 int size() { 396 return size(_length); 397 } 398 399 static int length_offset_in_bytes() { return (int) (offset_of(Array<T>, _length)); } 400 // Note, this offset don't have to be wordSize aligned. 401 static int base_offset_in_bytes() { return (int) (offset_of(Array<T>, _data)); }; 402 403 // FIXME: How to handle this? 404 void print_value_on(outputStream* st) const { 405 st->print("Array<T>(" INTPTR_FORMAT ")", p2i(this)); | 324 return (void*) Metaspace::allocate(loader_data, word_size, read_only, 325 MetaspaceObj::array_type(sizeof(T)), THREAD); 326 } 327 328 static size_t byte_sizeof(int length) { return sizeof(Array<T>) + MAX2(length - 1, 0) * sizeof(T); } 329 330 // WhiteBox API helper. 331 // Can't distinguish between array of length 0 and length 1, 332 // will always return 0 in those cases. 333 static int bytes_to_length(size_t bytes) { 334 assert(is_size_aligned(bytes, BytesPerWord), "Must be, for now"); 335 336 if (sizeof(Array<T>) >= bytes) { 337 return 0; 338 } 339 340 size_t left = bytes - sizeof(Array<T>); 341 assert(is_size_aligned(left, sizeof(T)), "Must be"); 342 343 size_t elements = left / sizeof(T); 344 assert(elements <= (size_t)INT_MAX, "number of elements " SIZE_FORMAT "doesn't fit into an int.", elements); 345 346 int length = (int)elements; 347 348 assert((size_t)size(length) * BytesPerWord == bytes, 349 "Expected: " SIZE_FORMAT " got: " SIZE_FORMAT, 350 bytes, (size_t)size(length) * BytesPerWord); 351 352 return length; 353 } 354 355 explicit Array(int length) : _length(length) { 356 assert(length >= 0, "illegal length"); 357 } 358 359 Array(int length, T init) : _length(length) { 360 assert(length >= 0, "illegal length"); 361 for (int i = 0; i < length; i++) { 362 _data[i] = init; 363 } 364 } 365 366 public: 367 368 // standard operations 369 int length() const { return _length; } 370 T* data() { return _data; } 371 bool is_empty() const { return length() == 0; } 372 373 int index_of(const T& x) const { 374 int i = length(); 375 while (i-- > 0 && _data[i] != x) ; 376 377 return i; 378 } 379 380 // sort the array. 381 bool contains(const T& x) const { return index_of(x) >= 0; } 382 383 T at(int i) const { assert(i >= 0 && i< _length, "oob: 0 <= %d < %d", i, _length); return _data[i]; } 384 void at_put(const int i, const T& x) { assert(i >= 0 && i< _length, "oob: 0 <= %d < %d", i, _length); _data[i] = x; } 385 T* adr_at(const int i) { assert(i >= 0 && i< _length, "oob: 0 <= %d < %d", i, _length); return &_data[i]; } 386 int find(const T& x) { return index_of(x); } 387 388 T at_acquire(const int which) { return OrderAccess::load_acquire(adr_at(which)); } 389 void release_at_put(int which, T contents) { OrderAccess::release_store(adr_at(which), contents); } 390 391 static int size(int length) { 392 return align_size_up(byte_sizeof(length), BytesPerWord) / BytesPerWord; 393 } 394 395 int size() { 396 return size(_length); 397 } 398 399 static int length_offset_in_bytes() { return (int) (offset_of(Array<T>, _length)); } 400 // Note, this offset don't have to be wordSize aligned. 401 static int base_offset_in_bytes() { return (int) (offset_of(Array<T>, _data)); }; 402 403 // FIXME: How to handle this? 404 void print_value_on(outputStream* st) const { 405 st->print("Array<T>(" INTPTR_FORMAT ")", p2i(this)); |