comparison gcc/memory-block.cc @ 145:1830386684a0

gcc-9.2.0
author anatofuz
date Thu, 13 Feb 2020 11:34:05 +0900
parents 84e7813d76e9
children
comparison
equal deleted inserted replaced
131:84e7813d76e9 145:1830386684a0
1 /* Shared pool of memory blocks for pool allocators. 1 /* Shared pool of memory blocks for pool allocators.
2 Copyright (C) 2015-2018 Free Software Foundation, Inc. 2 Copyright (C) 2015-2020 Free Software Foundation, Inc.
3 3
4 This file is part of GCC. 4 This file is part of GCC.
5 5
6 GCC is free software; you can redistribute it and/or modify it under 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 7 the terms of the GNU General Public License as published by the Free
26 /* Global singleton-like instance. */ 26 /* Global singleton-like instance. */
27 memory_block_pool memory_block_pool::instance; 27 memory_block_pool memory_block_pool::instance;
28 28
29 memory_block_pool::memory_block_pool () : m_blocks (NULL) {} 29 memory_block_pool::memory_block_pool () : m_blocks (NULL) {}
30 30
31 /* Return all blocks from free list to the OS. */ 31 /* Reduce free list to NUM blocks and return remaining to malloc. */
32 void 32 void
33 memory_block_pool::clear_free_list () 33 memory_block_pool::reduce_free_list (int num)
34 { 34 {
35 while (m_blocks) 35 block_list **blocks = &m_blocks;
36
37 /* First skip NUM blocks. */
38
39 for (;num > 0 && *blocks; num--)
40 blocks = &(*blocks)->m_next;
41
42 if (!*blocks)
43 return;
44
45 /* And free the remainder of them. */
46
47 block_list *to_free = *blocks;
48 *blocks = NULL;
49
50 while (to_free)
36 { 51 {
37 block_list *next = m_blocks->m_next; 52 block_list *next = to_free->m_next;
38 XDELETEVEC (m_blocks); 53 XDELETEVEC (to_free);
39 m_blocks = next; 54 to_free = next;
40 } 55 }
41 } 56 }
42 57
43 /* Allocate a chunk for obstack. Use the pool if requested chunk size matches 58 /* Allocate a chunk for obstack. Use the pool if requested chunk size matches
44 the size of blocks in the pool. */ 59 the size of blocks in the pool. */
60 if (size == memory_block_pool::block_size) 75 if (size == memory_block_pool::block_size)
61 memory_block_pool::release (chunk); 76 memory_block_pool::release (chunk);
62 else 77 else
63 XDELETEVEC (chunk); 78 XDELETEVEC (chunk);
64 } 79 }
80
81 /* Return allocated memory back to malloc (and to system). */
82 void
83 memory_block_pool::trim (int num)
84 {
85 instance.reduce_free_list (num);
86 }