simple_alloc.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * Implement primitive realloc(3) functionality.
  3. *
  4. * Author: Mark A. Greer <mgreer@mvista.com>
  5. *
  6. * 2006 (c) MontaVista, Software, Inc. This file is licensed under
  7. * the terms of the GNU General Public License version 2. This program
  8. * is licensed "as is" without any warranty of any kind, whether express
  9. * or implied.
  10. */
  11. #include <stddef.h>
  12. #include "types.h"
  13. #include "page.h"
  14. #include "string.h"
  15. #include "ops.h"
  16. #define ENTRY_BEEN_USED 0x01
  17. #define ENTRY_IN_USE 0x02
  18. static struct alloc_info {
  19. unsigned long flags;
  20. unsigned long base;
  21. unsigned long size;
  22. } *alloc_tbl;
  23. static unsigned long tbl_entries;
  24. static unsigned long alloc_min;
  25. static unsigned long next_base;
  26. static unsigned long space_left;
  27. /*
  28. * First time an entry is used, its base and size are set.
  29. * An entry can be freed and re-malloc'd but its base & size don't change.
  30. * Should be smart enough for needs of bootwrapper.
  31. */
  32. static void *simple_malloc(unsigned long size)
  33. {
  34. unsigned long i;
  35. struct alloc_info *p = alloc_tbl;
  36. if (size == 0)
  37. goto err_out;
  38. size = _ALIGN_UP(size, alloc_min);
  39. for (i=0; i<tbl_entries; i++, p++)
  40. if (!(p->flags & ENTRY_BEEN_USED)) { /* never been used */
  41. if (size <= space_left) {
  42. p->base = next_base;
  43. p->size = size;
  44. p->flags = ENTRY_BEEN_USED | ENTRY_IN_USE;
  45. next_base += size;
  46. space_left -= size;
  47. return (void *)p->base;
  48. }
  49. goto err_out; /* not enough space left */
  50. }
  51. /* reuse an entry keeping same base & size */
  52. else if (!(p->flags & ENTRY_IN_USE) && (size <= p->size)) {
  53. p->flags |= ENTRY_IN_USE;
  54. return (void *)p->base;
  55. }
  56. err_out:
  57. return NULL;
  58. }
  59. static struct alloc_info *simple_find_entry(void *ptr)
  60. {
  61. unsigned long i;
  62. struct alloc_info *p = alloc_tbl;
  63. for (i=0; i<tbl_entries; i++,p++) {
  64. if (!(p->flags & ENTRY_BEEN_USED))
  65. break;
  66. if ((p->flags & ENTRY_IN_USE) &&
  67. (p->base == (unsigned long)ptr))
  68. return p;
  69. }
  70. return NULL;
  71. }
  72. static void simple_free(void *ptr)
  73. {
  74. struct alloc_info *p = simple_find_entry(ptr);
  75. if (p != NULL)
  76. p->flags &= ~ENTRY_IN_USE;
  77. }
  78. /*
  79. * Change size of area pointed to by 'ptr' to 'size'.
  80. * If 'ptr' is NULL, then its a malloc(). If 'size' is 0, then its a free().
  81. * 'ptr' must be NULL or a pointer to a non-freed area previously returned by
  82. * simple_realloc() or simple_malloc().
  83. */
  84. static void *simple_realloc(void *ptr, unsigned long size)
  85. {
  86. struct alloc_info *p;
  87. void *new;
  88. if (size == 0) {
  89. simple_free(ptr);
  90. return NULL;
  91. }
  92. if (ptr == NULL)
  93. return simple_malloc(size);
  94. p = simple_find_entry(ptr);
  95. if (p == NULL) /* ptr not from simple_malloc/simple_realloc */
  96. return NULL;
  97. if (size <= p->size) /* fits in current block */
  98. return ptr;
  99. new = simple_malloc(size);
  100. memcpy(new, ptr, p->size);
  101. simple_free(ptr);
  102. return new;
  103. }
  104. /*
  105. * Returns addr of first byte after heap so caller can see if it took
  106. * too much space. If so, change args & try again.
  107. */
  108. void *simple_alloc_init(char *base, unsigned long heap_size,
  109. unsigned long granularity, unsigned long max_allocs)
  110. {
  111. unsigned long heap_base, tbl_size;
  112. heap_size = _ALIGN_UP(heap_size, granularity);
  113. alloc_min = granularity;
  114. tbl_entries = max_allocs;
  115. tbl_size = tbl_entries * sizeof(struct alloc_info);
  116. alloc_tbl = (struct alloc_info *)_ALIGN_UP((unsigned long)base, 8);
  117. memset(alloc_tbl, 0, tbl_size);
  118. heap_base = _ALIGN_UP((unsigned long)alloc_tbl + tbl_size, alloc_min);
  119. next_base = heap_base;
  120. space_left = heap_size;
  121. platform_ops.malloc = simple_malloc;
  122. platform_ops.free = simple_free;
  123. platform_ops.realloc = simple_realloc;
  124. return (void *)(heap_base + heap_size);
  125. }