mempool.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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 <stdio.h>
  35. #include <stdlib.h>
  36. #include <string.h>
  37. #include <errno.h>
  38. #include "../include/list.h"
  39. #include "mempool.h"
  40. int mempool_bytes = 0;
  41. struct mempool_list {
  42. struct list_head free;
  43. short free_entries;
  44. short used_entries;
  45. };
  46. struct mempool_entry {
  47. struct list_head list;
  48. int mempool_entry;
  49. char mem[0];
  50. };
  51. struct mempool_list mempool_group[MEMPOOL_GROUP_SIZE];
  52. #ifdef MEMPOOL_ON
  53. int mempool_init (int pool_sizes[MEMPOOL_GROUP_SIZE])
  54. {
  55. int i, j;
  56. struct mempool_entry *entry;
  57. void *mempool;
  58. char *p;
  59. int bytes_to_alloc;
  60. for (i = 0; i < MEMPOOL_GROUP_SIZE; i++) {
  61. for (j = 0; j < pool_sizes[i]; j++) {
  62. bytes_to_alloc = sizeof (struct mempool_entry) + (1 << i) + 3;
  63. bytes_to_alloc &= 0xFFFFFFFC;
  64. mempool_bytes += bytes_to_alloc;
  65. }
  66. }
  67. mempool = malloc (mempool_bytes);
  68. if (mempool == 0) {
  69. return (ENOMEM);
  70. }
  71. memset (mempool, 0, mempool_bytes);
  72. for (p = (char *)mempool, i = 0; i < MEMPOOL_GROUP_SIZE; i++) {
  73. list_init (&mempool_group[i].free);
  74. mempool_group[i].free_entries = pool_sizes[i];
  75. mempool_group[i].used_entries = 0;
  76. for (j = 0; j < pool_sizes[i]; j++) {
  77. entry = (struct mempool_entry *)p;
  78. entry->mempool_entry = i;
  79. list_add (&entry->list, &mempool_group[i].free);
  80. bytes_to_alloc = sizeof (struct mempool_entry) + (1 << i) + 3;
  81. bytes_to_alloc &= 0xFFFFFFFC;
  82. p += bytes_to_alloc;
  83. }
  84. }
  85. return (0);
  86. }
  87. void *mempool_malloc (size_t size)
  88. {
  89. struct mempool_entry *mempool_entry;
  90. int i;
  91. #ifdef DEBUG
  92. int first = 0;
  93. int stats_inuse[MEMPOOL_GROUP_SIZE];
  94. int stats_avail[MEMPOOL_GROUP_SIZE];
  95. int stats_memoryused[MEMPOOL_GROUP_SIZE];
  96. #endif
  97. for (i = 0; i < MEMPOOL_GROUP_SIZE; i++) {
  98. #ifdef DEBUG
  99. if (((i << 1) >= size) && first == 0) {
  100. first = i;
  101. }
  102. #endif
  103. if (((1 << i) >= size) &&
  104. mempool_group[i].free_entries) {
  105. mempool_group[i].used_entries += 1;
  106. mempool_group[i].free_entries -= 1;
  107. mempool_entry = list_entry (mempool_group[i].free.next,
  108. struct mempool_entry, list);
  109. list_del (mempool_group[i].free.next);
  110. return (&mempool_entry->mem);
  111. }
  112. }
  113. #ifdef DEBUG
  114. mempool_getstats (stats_inuse, stats_avail, stats_memoryused);
  115. printf ("MEMORY POOLS first %d %d:\n", first, size);
  116. for (i = 0; i < MEMPOOL_GROUP_SIZE; i++) {
  117. printf ("order %d size %d inuse %d avail %d memory used %d\n",
  118. i, 1<<i, stats_inuse[i], stats_avail[i], stats_memoryused[i]);
  119. }
  120. #endif
  121. return (0);
  122. }
  123. void mempool_free (void *ptr) {
  124. struct mempool_entry *mempool_entry;
  125. mempool_entry = ((struct mempool_entry *)((unsigned long)(ptr) - (unsigned long)(&((struct mempool_entry *)0)->mem)));
  126. mempool_group[mempool_entry->mempool_entry].free_entries += 1;
  127. mempool_group[mempool_entry->mempool_entry].used_entries -= 1;
  128. list_add (&mempool_entry->list, &mempool_group[mempool_entry->mempool_entry].free);
  129. }
  130. void *mempool_realloc (void *ptr, size_t size) {
  131. struct mempool_entry *mempool_entry;
  132. void *new_ptr;
  133. mempool_entry = ((struct mempool_entry *)((unsigned long)(ptr) - (unsigned long)(&((struct mempool_entry *)0)->mem)));
  134. if (ptr == 0 || (1 << mempool_entry->mempool_entry) < size) {
  135. /*
  136. * Must grow allocated block, copy memory, free old block
  137. */
  138. new_ptr = (void *)mempool_malloc (size);
  139. if (new_ptr == 0) {
  140. return (0);
  141. }
  142. if (ptr) {
  143. memcpy (new_ptr, ptr, (1 << mempool_entry->mempool_entry));
  144. mempool_free (ptr);
  145. }
  146. ptr = new_ptr;
  147. }
  148. return (ptr);
  149. }
  150. char *mempool_strdup (const char *s)
  151. {
  152. char *mem;
  153. mem = mempool_malloc (strlen (s));
  154. strcpy (mem, s);
  155. return (mem);
  156. }
  157. void mempool_getstats (
  158. int stats_inuse[MEMPOOL_GROUP_SIZE],
  159. int stats_avail[MEMPOOL_GROUP_SIZE],
  160. int stats_memoryused[MEMPOOL_GROUP_SIZE])
  161. {
  162. int i;
  163. for (i = 0; i < MEMPOOL_GROUP_SIZE; i++) {
  164. stats_inuse[i] = mempool_group[i].used_entries;
  165. stats_avail[i] = mempool_group[i].free_entries;
  166. stats_memoryused[i] = (mempool_group[i].used_entries + mempool_group[i].free_entries) * (sizeof (struct mempool_entry) + (1<<i));
  167. }
  168. }
  169. #else /* MEMPOOL_ON NOT SET */
  170. int mempool_init (int pool_sizes[MEMPOOL_GROUP_SIZE]) {
  171. return (0);
  172. }
  173. void *mempool_malloc (size_t size) {
  174. return (malloc (size));
  175. }
  176. void mempool_free (void *ptr) {
  177. free (ptr);
  178. }
  179. void *mempool_realloc (void *ptr, size_t size) {
  180. return (realloc (ptr, size));
  181. }
  182. char *mempool_strdup (const char *s) {
  183. return (strdup (s));
  184. }
  185. void mempool_getstats (
  186. int stats_inuse[MEMPOOL_GROUP_SIZE],
  187. int stats_avail[MEMPOOL_GROUP_SIZE],
  188. int stats_memoryused[MEMPOOL_GROUP_SIZE]) {
  189. return;
  190. }
  191. #endif