simple_alloc.c 3.4 KB

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