345
346 BlockTree() : _root(nullptr) {}
347
348 // Add a memory block to the tree. Its content will be overwritten.
349 void add_block(MetaWord* p, size_t word_size) {
350 DEBUG_ONLY(zap_range(p, word_size));
351 assert(word_size >= MinWordSize, "invalid block size " SIZE_FORMAT, word_size);
352 Node* n = new(p) Node(word_size);
353 if (_root == nullptr) {
354 _root = n;
355 } else {
356 insert(_root, n);
357 }
358 _counter.add(word_size);
359 }
360
361 // Given a word_size, search and return the smallest block that is equal or
362 // larger than that size. Upon return, *p_real_word_size contains the actual
363 // block size.
364 MetaWord* remove_block(size_t word_size, size_t* p_real_word_size) {
365 assert(word_size >= MinWordSize, "invalid block size " SIZE_FORMAT, word_size);
366
367 Node* n = find_closest_fit(word_size);
368
369 if (n != nullptr) {
370 DEBUG_ONLY(check_node(n);)
371 assert(n->_word_size >= word_size, "sanity");
372
373 if (n->_next != nullptr) {
374 // If the node is head of a chain of same sized nodes, we leave it alone
375 // and instead remove one of the follow up nodes (which is simpler than
376 // removing the chain head node and then having to graft the follow up
377 // node into its place in the tree).
378 n = remove_from_list(n);
379 } else {
380 remove_node_from_tree(n);
381 }
382
383 MetaWord* p = (MetaWord*)n;
384 *p_real_word_size = n->_word_size;
385
|
345
346 BlockTree() : _root(nullptr) {}
347
348 // Add a memory block to the tree. Its content will be overwritten.
349 void add_block(MetaWord* p, size_t word_size) {
350 DEBUG_ONLY(zap_range(p, word_size));
351 assert(word_size >= MinWordSize, "invalid block size " SIZE_FORMAT, word_size);
352 Node* n = new(p) Node(word_size);
353 if (_root == nullptr) {
354 _root = n;
355 } else {
356 insert(_root, n);
357 }
358 _counter.add(word_size);
359 }
360
361 // Given a word_size, search and return the smallest block that is equal or
362 // larger than that size. Upon return, *p_real_word_size contains the actual
363 // block size.
364 MetaWord* remove_block(size_t word_size, size_t* p_real_word_size) {
365 assert(word_size > 0, "invalid block size " SIZE_FORMAT, word_size);
366
367 Node* n = find_closest_fit(word_size);
368
369 if (n != nullptr) {
370 DEBUG_ONLY(check_node(n);)
371 assert(n->_word_size >= word_size, "sanity");
372
373 if (n->_next != nullptr) {
374 // If the node is head of a chain of same sized nodes, we leave it alone
375 // and instead remove one of the follow up nodes (which is simpler than
376 // removing the chain head node and then having to graft the follow up
377 // node into its place in the tree).
378 n = remove_from_list(n);
379 } else {
380 remove_node_from_tree(n);
381 }
382
383 MetaWord* p = (MetaWord*)n;
384 *p_real_word_size = n->_word_size;
385
|