xref: /netbsd-src/external/gpl3/gcc.old/dist/gcc/memory-block.h (revision 8feb0f0b7eaff0608f8350bbfa3098827b4bb91b)
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
71debfc3dSmrg it under the terms of the GNU General Public License as published by
81debfc3dSmrg the Free Software Foundation; either version 3, or (at your option)
91debfc3dSmrg any later version.
101debfc3dSmrg 
111debfc3dSmrg GCC is distributed in the hope that it will be useful,
121debfc3dSmrg but WITHOUT ANY WARRANTY; without even the implied warranty of
131debfc3dSmrg MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
141debfc3dSmrg GNU General Public License 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 
211debfc3dSmrg #ifndef MEMORY_BLOCK_H
221debfc3dSmrg #define MEMORY_BLOCK_H
231debfc3dSmrg 
241debfc3dSmrg /* Shared pool which allows other memory pools to reuse each others' allocated
251debfc3dSmrg    memory blocks instead of calling free/malloc again.  */
261debfc3dSmrg class memory_block_pool
271debfc3dSmrg {
281debfc3dSmrg public:
291debfc3dSmrg   /* Blocks have fixed size.  This is necessary for sharing.  */
301debfc3dSmrg   static const size_t block_size = 64 * 1024;
31*8feb0f0bSmrg   /* Number of blocks we keep in the freelists.  */
32*8feb0f0bSmrg   static const size_t freelist_size = 1024 * 1024 / block_size;
331debfc3dSmrg 
341debfc3dSmrg   memory_block_pool ();
351debfc3dSmrg 
361debfc3dSmrg   static inline void *allocate () ATTRIBUTE_MALLOC;
371debfc3dSmrg   static inline void release (void *);
38*8feb0f0bSmrg   static void trim (int nblocks = freelist_size);
39*8feb0f0bSmrg   void reduce_free_list (int);
401debfc3dSmrg 
411debfc3dSmrg private:
421debfc3dSmrg   /* memory_block_pool singleton instance, defined in memory-block.cc.  */
431debfc3dSmrg   static memory_block_pool instance;
441debfc3dSmrg 
451debfc3dSmrg   struct block_list
461debfc3dSmrg   {
471debfc3dSmrg     block_list *m_next;
481debfc3dSmrg   };
491debfc3dSmrg 
501debfc3dSmrg   /* Free list.  */
511debfc3dSmrg   block_list *m_blocks;
521debfc3dSmrg };
531debfc3dSmrg 
541debfc3dSmrg /* Allocate a single block.  Reuse a previously returned block, if possible.  */
551debfc3dSmrg inline void *
allocate()561debfc3dSmrg memory_block_pool::allocate ()
571debfc3dSmrg {
581debfc3dSmrg   if (instance.m_blocks == NULL)
591debfc3dSmrg     return XNEWVEC (char, block_size);
601debfc3dSmrg 
611debfc3dSmrg   void *result = instance.m_blocks;
621debfc3dSmrg   instance.m_blocks = instance.m_blocks->m_next;
631debfc3dSmrg   VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (result, block_size));
641debfc3dSmrg   return result;
651debfc3dSmrg }
661debfc3dSmrg 
671debfc3dSmrg /* Return UNCAST_BLOCK to the pool.  */
681debfc3dSmrg inline void
release(void * uncast_block)691debfc3dSmrg memory_block_pool::release (void *uncast_block)
701debfc3dSmrg {
711debfc3dSmrg   block_list *block = new (uncast_block) block_list;
721debfc3dSmrg   block->m_next = instance.m_blocks;
731debfc3dSmrg   instance.m_blocks = block;
74c0a68be4Smrg 
75c0a68be4Smrg   VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS ((char *)uncast_block
76c0a68be4Smrg 						+ sizeof (block_list),
77c0a68be4Smrg 						block_size
78c0a68be4Smrg 						- sizeof (block_list)));
791debfc3dSmrg }
801debfc3dSmrg 
811debfc3dSmrg extern void *mempool_obstack_chunk_alloc (size_t) ATTRIBUTE_MALLOC;
821debfc3dSmrg extern void mempool_obstack_chunk_free (void *);
831debfc3dSmrg 
841debfc3dSmrg #endif /* MEMORY_BLOCK_H */
85