4
0

mempool.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /*
  2. * Copyright (c) 2003-2004 MontaVista Software, Inc.
  3. *
  4. * All rights reserved.
  5. *
  6. * Author: Steven Dake (sdake@redhat.com)
  7. *
  8. * This software licensed under BSD license, the text of which follows:
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions are met:
  12. *
  13. * - Redistributions of source code must retain the above copyright notice,
  14. * this list of conditions and the following disclaimer.
  15. * - Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  26. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  27. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  28. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  30. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  31. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  32. * THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. #include <config.h>
  35. #include <stdio.h>
  36. #include <stdlib.h>
  37. #include <string.h>
  38. #include <errno.h>
  39. #include <corosync/list.h>
  40. #include "mempool.h"
  41. int mempool_bytes = 0;
  42. struct mempool_list {
  43. struct list_head free;
  44. short free_entries;
  45. short used_entries;
  46. };
  47. struct mempool_entry {
  48. struct list_head list;
  49. int mempool_entry;
  50. char mem[0];
  51. };
  52. struct mempool_list mempool_group[MEMPOOL_GROUP_SIZE];
  53. #ifdef MEMPOOL_ON
  54. int mempool_init (int pool_sizes[MEMPOOL_GROUP_SIZE])
  55. {
  56. int i, j;
  57. struct mempool_entry *entry;
  58. void *mempool;
  59. char *p;
  60. int bytes_to_alloc;
  61. for (i = 0; i < MEMPOOL_GROUP_SIZE; i++) {
  62. for (j = 0; j < pool_sizes[i]; j++) {
  63. bytes_to_alloc = sizeof (struct mempool_entry) + (1 << i) + 3;
  64. bytes_to_alloc &= 0xFFFFFFFC;
  65. mempool_bytes += bytes_to_alloc;
  66. }
  67. }
  68. mempool = malloc (mempool_bytes);
  69. if (mempool == 0) {
  70. return (ENOMEM);
  71. }
  72. memset (mempool, 0, mempool_bytes);
  73. for (p = (char *)mempool, i = 0; i < MEMPOOL_GROUP_SIZE; i++) {
  74. list_init (&mempool_group[i].free);
  75. mempool_group[i].free_entries = pool_sizes[i];
  76. mempool_group[i].used_entries = 0;
  77. for (j = 0; j < pool_sizes[i]; j++) {
  78. entry = (struct mempool_entry *)p;
  79. entry->mempool_entry = i;
  80. list_add (&entry->list, &mempool_group[i].free);
  81. bytes_to_alloc = sizeof (struct mempool_entry) + (1 << i) + 3;
  82. bytes_to_alloc &= 0xFFFFFFFC;
  83. p += bytes_to_alloc;
  84. }
  85. }
  86. return (0);
  87. }
  88. void *mempool_malloc (size_t size)
  89. {
  90. struct mempool_entry *mempool_entry;
  91. int i;
  92. #ifdef DEBUG
  93. int first = 0;
  94. int stats_inuse[MEMPOOL_GROUP_SIZE];
  95. int stats_avail[MEMPOOL_GROUP_SIZE];
  96. int stats_memoryused[MEMPOOL_GROUP_SIZE];
  97. #endif
  98. for (i = 0; i < MEMPOOL_GROUP_SIZE; i++) {
  99. #ifdef DEBUG
  100. if (((i << 1) >= size) && first == 0) {
  101. first = i;
  102. }
  103. #endif
  104. if (((1 << i) >= size) &&
  105. mempool_group[i].free_entries) {
  106. mempool_group[i].used_entries += 1;
  107. mempool_group[i].free_entries -= 1;
  108. mempool_entry = list_entry (mempool_group[i].free.next,
  109. struct mempool_entry, list);
  110. list_del (mempool_group[i].free.next);
  111. return (&mempool_entry->mem);
  112. }
  113. }
  114. #ifdef DEBUG
  115. mempool_getstats (stats_inuse, stats_avail, stats_memoryused);
  116. printf ("MEMORY POOLS first %d %d:\n", first, size);
  117. for (i = 0; i < MEMPOOL_GROUP_SIZE; i++) {
  118. printf ("order %d size %d inuse %d avail %d memory used %d\n",
  119. i, 1<<i, stats_inuse[i], stats_avail[i], stats_memoryused[i]);
  120. }
  121. #endif
  122. return (0);
  123. }
  124. void mempool_free (void *ptr) {
  125. struct mempool_entry *mempool_entry;
  126. mempool_entry = ((struct mempool_entry *)((unsigned long)(ptr) - (unsigned long)(&((struct mempool_entry *)0)->mem)));
  127. mempool_group[mempool_entry->mempool_entry].free_entries += 1;
  128. mempool_group[mempool_entry->mempool_entry].used_entries -= 1;
  129. list_add (&mempool_entry->list, &mempool_group[mempool_entry->mempool_entry].free);
  130. }
  131. void *mempool_realloc (void *ptr, size_t size) {
  132. struct mempool_entry *mempool_entry;
  133. void *new_ptr;
  134. mempool_entry = ((struct mempool_entry *)((unsigned long)(ptr) - (unsigned long)(&((struct mempool_entry *)0)->mem)));
  135. if (ptr == 0 || (1 << mempool_entry->mempool_entry) < size) {
  136. /*
  137. * Must grow allocated block, copy memory, free old block
  138. */
  139. new_ptr = (void *)mempool_malloc (size);
  140. if (new_ptr == 0) {
  141. return (0);
  142. }
  143. if (ptr) {
  144. memcpy (new_ptr, ptr, (1 << mempool_entry->mempool_entry));
  145. mempool_free (ptr);
  146. }
  147. ptr = new_ptr;
  148. }
  149. return (ptr);
  150. }
  151. char *mempool_strdup (const char *s)
  152. {
  153. char *mem;
  154. mem = mempool_malloc (strlen (s));
  155. strcpy (mem, s);
  156. return (mem);
  157. }
  158. void mempool_getstats (
  159. int stats_inuse[MEMPOOL_GROUP_SIZE],
  160. int stats_avail[MEMPOOL_GROUP_SIZE],
  161. int stats_memoryused[MEMPOOL_GROUP_SIZE])
  162. {
  163. int i;
  164. for (i = 0; i < MEMPOOL_GROUP_SIZE; i++) {
  165. stats_inuse[i] = mempool_group[i].used_entries;
  166. stats_avail[i] = mempool_group[i].free_entries;
  167. stats_memoryused[i] = (mempool_group[i].used_entries + mempool_group[i].free_entries) * (sizeof (struct mempool_entry) + (1<<i));
  168. }
  169. }
  170. #else /* MEMPOOL_ON NOT SET */
  171. int mempool_init (int pool_sizes[MEMPOOL_GROUP_SIZE]) {
  172. return (0);
  173. }
  174. void *mempool_malloc (size_t size) {
  175. return (malloc (size));
  176. }
  177. void mempool_free (void *ptr) {
  178. free (ptr);
  179. }
  180. void *mempool_realloc (void *ptr, size_t size) {
  181. return (realloc (ptr, size));
  182. }
  183. char *mempool_strdup (const char *s) {
  184. return (strdup (s));
  185. }
  186. void mempool_getstats (
  187. int stats_inuse[MEMPOOL_GROUP_SIZE],
  188. int stats_avail[MEMPOOL_GROUP_SIZE],
  189. int stats_memoryused[MEMPOOL_GROUP_SIZE]) {
  190. return;
  191. }
  192. #endif