1 /* Shared pool of memory blocks for pool allocators. 2 Copyright (C) 2015-2019 Free Software Foundation, Inc. 3 4 This file is part of GCC. 5 6 GCC is free software; you can redistribute it and/or modify it under 7 the terms of the GNU General Public License as published by the Free 8 Software Foundation; either version 3, or (at your option) any later 9 version. 10 11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY 12 WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 for more details. 15 16 You should have received a copy of the GNU General Public License 17 along with GCC; see the file COPYING3. If not see 18 <http://www.gnu.org/licenses/>. */ 19 20 #include "config.h" 21 #include "system.h" 22 #include "coretypes.h" 23 #include "memory-block.h" 24 #include "obstack.h" 25 26 /* Global singleton-like instance. */ 27 memory_block_pool memory_block_pool::instance; 28 memory_block_pool()29memory_block_pool::memory_block_pool () : m_blocks (NULL) {} 30 31 /* Return all blocks from free list to the OS. */ 32 void clear_free_list()33memory_block_pool::clear_free_list () 34 { 35 while (m_blocks) 36 { 37 block_list *next = m_blocks->m_next; 38 XDELETEVEC (m_blocks); 39 m_blocks = next; 40 } 41 } 42 43 /* Allocate a chunk for obstack. Use the pool if requested chunk size matches 44 the size of blocks in the pool. */ 45 void * mempool_obstack_chunk_alloc(size_t size)46mempool_obstack_chunk_alloc (size_t size) 47 { 48 if (size == memory_block_pool::block_size) 49 return memory_block_pool::allocate (); 50 else 51 return XNEWVEC (char, size); 52 } 53 54 /* Free previously allocated obstack chunk. */ 55 void mempool_obstack_chunk_free(void * chunk)56mempool_obstack_chunk_free (void *chunk) 57 { 58 size_t size = (reinterpret_cast<_obstack_chunk *> (chunk)->limit 59 - reinterpret_cast<char *> (chunk)); 60 if (size == memory_block_pool::block_size) 61 memory_block_pool::release (chunk); 62 else 63 XDELETEVEC (chunk); 64 } 65