11debfc3dSmrg /* Shared pool of memory blocks for pool allocators. 2*8feb0f0bSmrg Copyright (C) 2015-2020 Free Software Foundation, Inc. 31debfc3dSmrg 41debfc3dSmrg This file is part of GCC. 51debfc3dSmrg 61debfc3dSmrg GCC is free software; you can redistribute it and/or modify it under 71debfc3dSmrg the terms of the GNU General Public License as published by the Free 81debfc3dSmrg Software Foundation; either version 3, or (at your option) any later 91debfc3dSmrg version. 101debfc3dSmrg 111debfc3dSmrg GCC is distributed in the hope that it will be useful, but WITHOUT ANY 121debfc3dSmrg WARRANTY; without even the implied warranty of MERCHANTABILITY or 131debfc3dSmrg FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 141debfc3dSmrg for more details. 151debfc3dSmrg 161debfc3dSmrg You should have received a copy of the GNU General Public License 171debfc3dSmrg along with GCC; see the file COPYING3. If not see 181debfc3dSmrg <http://www.gnu.org/licenses/>. */ 191debfc3dSmrg 201debfc3dSmrg #include "config.h" 211debfc3dSmrg #include "system.h" 221debfc3dSmrg #include "coretypes.h" 231debfc3dSmrg #include "memory-block.h" 241debfc3dSmrg #include "obstack.h" 251debfc3dSmrg 261debfc3dSmrg /* Global singleton-like instance. */ 271debfc3dSmrg memory_block_pool memory_block_pool::instance; 281debfc3dSmrg memory_block_pool()291debfc3dSmrgmemory_block_pool::memory_block_pool () : m_blocks (NULL) {} 301debfc3dSmrg 31*8feb0f0bSmrg /* Reduce free list to NUM blocks and return remaining to malloc. */ 321debfc3dSmrg void reduce_free_list(int num)33*8feb0f0bSmrgmemory_block_pool::reduce_free_list (int num) 341debfc3dSmrg { 35*8feb0f0bSmrg block_list **blocks = &m_blocks; 36*8feb0f0bSmrg 37*8feb0f0bSmrg /* First skip NUM blocks. */ 38*8feb0f0bSmrg 39*8feb0f0bSmrg for (;num > 0 && *blocks; num--) 40*8feb0f0bSmrg blocks = &(*blocks)->m_next; 41*8feb0f0bSmrg 42*8feb0f0bSmrg if (!*blocks) 43*8feb0f0bSmrg return; 44*8feb0f0bSmrg 45*8feb0f0bSmrg /* And free the remainder of them. */ 46*8feb0f0bSmrg 47*8feb0f0bSmrg block_list *to_free = *blocks; 48*8feb0f0bSmrg *blocks = NULL; 49*8feb0f0bSmrg 50*8feb0f0bSmrg while (to_free) 511debfc3dSmrg { 52*8feb0f0bSmrg block_list *next = to_free->m_next; 53*8feb0f0bSmrg XDELETEVEC (to_free); 54*8feb0f0bSmrg to_free = next; 551debfc3dSmrg } 561debfc3dSmrg } 571debfc3dSmrg 581debfc3dSmrg /* Allocate a chunk for obstack. Use the pool if requested chunk size matches 591debfc3dSmrg the size of blocks in the pool. */ 601debfc3dSmrg void * mempool_obstack_chunk_alloc(size_t size)611debfc3dSmrgmempool_obstack_chunk_alloc (size_t size) 621debfc3dSmrg { 631debfc3dSmrg if (size == memory_block_pool::block_size) 641debfc3dSmrg return memory_block_pool::allocate (); 651debfc3dSmrg else 661debfc3dSmrg return XNEWVEC (char, size); 671debfc3dSmrg } 681debfc3dSmrg 691debfc3dSmrg /* Free previously allocated obstack chunk. */ 701debfc3dSmrg void mempool_obstack_chunk_free(void * chunk)711debfc3dSmrgmempool_obstack_chunk_free (void *chunk) 721debfc3dSmrg { 731debfc3dSmrg size_t size = (reinterpret_cast<_obstack_chunk *> (chunk)->limit 741debfc3dSmrg - reinterpret_cast<char *> (chunk)); 751debfc3dSmrg if (size == memory_block_pool::block_size) 761debfc3dSmrg memory_block_pool::release (chunk); 771debfc3dSmrg else 781debfc3dSmrg XDELETEVEC (chunk); 791debfc3dSmrg } 80*8feb0f0bSmrg 81*8feb0f0bSmrg /* Return allocated memory back to malloc (and to system). */ 82*8feb0f0bSmrg void trim(int num)83*8feb0f0bSmrgmemory_block_pool::trim (int num) 84*8feb0f0bSmrg { 85*8feb0f0bSmrg instance.reduce_free_list (num); 86*8feb0f0bSmrg } 87