tiny.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. /* Licensed under LGPLv2.1+ - see LICENSE file for details */
  2. #include "tiny.h"
  3. #include "bitops.h"
  4. #include <assert.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. /* One byte header, one byte data. */
  8. #define MIN_BLOCK_SIZE 2
  9. /* Bit 7 (in any byte) == start or end. */
  10. #define TERM_BIT 0x80
  11. /* Bit 6 (first and last byte) == one byte long. */
  12. #define SINGLE_BYTE 0x40
  13. /* Bit 5 (of first byte) == "is this block free?" */
  14. #define FREE_BIT 0x20
  15. #define MAX_FREE_CACHED_SIZE 256
  16. /* Val is usually offset by MIN_BLOCK_SIZE here. */
  17. static unsigned encode_length(unsigned long val)
  18. {
  19. unsigned int bits = afls(val);
  20. /* 5 bits in first byte. */
  21. if (bits <= 5)
  22. return 1;
  23. /* 6 bits in last byte, 7 bits in middle ones. */
  24. return 2 + (bits - 5) / 7;
  25. }
  26. /* Header is included in length, so we might need an extra byte. */
  27. static unsigned encode_len_with_header(unsigned long len)
  28. {
  29. unsigned int hdrlen = 1;
  30. assert(len);
  31. while (encode_length(len + hdrlen - MIN_BLOCK_SIZE) != hdrlen)
  32. hdrlen = encode_length(len + hdrlen - MIN_BLOCK_SIZE);
  33. return hdrlen;
  34. }
  35. /* Encoding can be read from front or back; 0 is invalid at either
  36. * start or end. Returns bytes used for header. */
  37. static unsigned encode(unsigned long len, bool free, unsigned char arr[])
  38. {
  39. unsigned int hdrlen = 1;
  40. /* We can never have a length < MIN_BLOCK_SIZE. */
  41. assert(len >= MIN_BLOCK_SIZE);
  42. len -= MIN_BLOCK_SIZE;
  43. /* First byte always contains free bit. */
  44. arr[0] = TERM_BIT | (free ? FREE_BIT : 0);
  45. /* Also holds 5 bits of data (0 - 31) */
  46. arr[0] |= (len & 0x1F);
  47. len >>= 5;
  48. /* One byte only? */
  49. if (!len) {
  50. arr[0] |= SINGLE_BYTE;
  51. return hdrlen;
  52. }
  53. /* Middle bytes. */
  54. while (len >= (1 << 6)) {
  55. /* Next 7 data bits */
  56. arr[hdrlen++] = (len & 0x7F);
  57. len >>= 7;
  58. }
  59. arr[hdrlen++] = (len | TERM_BIT);
  60. return hdrlen;
  61. }
  62. /* Returns bytes used for header. */
  63. static unsigned decode(unsigned long *len, bool *free, const unsigned char *arr)
  64. {
  65. unsigned int hdrlen = 0, bits = 5;
  66. /* Free flag is in bit 5 */
  67. *free = (arr[hdrlen] & FREE_BIT);
  68. /* Bottom five bits are data. */
  69. *len = (arr[hdrlen] & 0x1f);
  70. if (!(arr[hdrlen++] & SINGLE_BYTE)) {
  71. /* Multi-byte encoding? */
  72. while (!(arr[hdrlen] & TERM_BIT)) {
  73. /* 7 more data bits. */
  74. *len |= (arr[hdrlen] & 0x7fUL) << bits;
  75. hdrlen++;
  76. bits += 7;
  77. }
  78. /* Final byte has 6 bits. */
  79. *len |= (arr[hdrlen] & 0x3fUL) << bits;
  80. hdrlen++;
  81. }
  82. *len += MIN_BLOCK_SIZE;
  83. return hdrlen;
  84. }
  85. /* We keep a helper array for freed mem, one byte per k. */
  86. static unsigned long free_array_size(unsigned long poolsize)
  87. {
  88. return poolsize / 1024;
  89. }
  90. /* We have series of 69 free sizes like so:
  91. * 1, 2, 3, 4. 6, 8, 10, 12, 14, 16. 20, 24, 28, 32... 252.
  92. */
  93. static unsigned long free_array_off(unsigned long size)
  94. {
  95. unsigned long off;
  96. if (size <= 4)
  97. off = size - 1;
  98. else if (size <= 16)
  99. off = size / 2 + 1;
  100. else
  101. off = size / 4 + 5;
  102. off *= 3;
  103. return off;
  104. }
  105. void tiny_alloc_init(void *pool, unsigned long poolsize)
  106. {
  107. /* We start with free array, and then the rest is free. */
  108. unsigned long arrsize = free_array_size(poolsize);
  109. /* Do nothing with 1 byte or less! */
  110. if (poolsize < MIN_BLOCK_SIZE)
  111. return;
  112. memset(pool, 0, arrsize);
  113. encode(poolsize - arrsize, true, (unsigned char *)pool + arrsize);
  114. }
  115. /* Walk through and try to coalesce */
  116. static bool try_coalesce(unsigned char *pool, unsigned long poolsize)
  117. {
  118. unsigned long len, prev_off = 0, prev_len = 0, off;
  119. bool free, prev_free = false, coalesced = false;
  120. off = free_array_size(poolsize);
  121. do {
  122. decode(&len, &free, pool + off);
  123. if (free && prev_free) {
  124. prev_len += len;
  125. encode(prev_len, true, pool + prev_off);
  126. coalesced = true;
  127. } else {
  128. prev_free = free;
  129. prev_off = off;
  130. prev_len = len;
  131. }
  132. off += len;
  133. } while (off < poolsize);
  134. /* Clear the free array. */
  135. if (coalesced)
  136. memset(pool, 0, free_array_size(poolsize));
  137. return coalesced;
  138. }
  139. static bool long_enough(unsigned long offset, unsigned long len,
  140. unsigned long size, unsigned long align)
  141. {
  142. unsigned long end = offset + len;
  143. offset += encode_len_with_header(len);
  144. offset = align_up(offset, align);
  145. return offset + size <= end;
  146. }
  147. static void add_to_free_array(unsigned char *arr,
  148. unsigned long poolsize,
  149. unsigned long size,
  150. unsigned long off)
  151. {
  152. unsigned long fa_off;
  153. if (size >= MAX_FREE_CACHED_SIZE)
  154. return;
  155. for (fa_off = free_array_off(size);
  156. fa_off + 3 < free_array_size(poolsize);
  157. fa_off += free_array_off(MAX_FREE_CACHED_SIZE)) {
  158. if (!arr[fa_off] && !arr[fa_off+1] && !arr[fa_off+2]) {
  159. arr[fa_off] = (off >> 16);
  160. arr[fa_off+1] = (off >> 8);
  161. arr[fa_off+2] = off;
  162. break;
  163. }
  164. }
  165. }
  166. void *tiny_alloc_get(void *pool, unsigned long poolsize,
  167. unsigned long size, unsigned long align)
  168. {
  169. unsigned long arrsize = free_array_size(poolsize);
  170. unsigned long len, off, actual, hdr, free_bucket;
  171. long fa_off;
  172. unsigned char *arr = pool;
  173. bool free, coalesced = false;
  174. /* We can't do anything with tiny pools. */
  175. if (poolsize < MIN_BLOCK_SIZE)
  176. return NULL;
  177. /* We don't do zero-allocs; allows 1 more offset in encoding. */
  178. if (!size)
  179. size = 1;
  180. /* Look for entries in free array, starting from right size up. */
  181. for (free_bucket = free_array_off(size);
  182. free_bucket < free_array_off(MAX_FREE_CACHED_SIZE);
  183. free_bucket += 3) {
  184. for (fa_off = free_bucket;
  185. fa_off + 3 < free_array_size(poolsize);
  186. fa_off += free_array_off(MAX_FREE_CACHED_SIZE)) {
  187. off = ((unsigned long)arr[fa_off]) << 16
  188. | ((unsigned long)arr[fa_off+1]) << 8
  189. | ((unsigned long)arr[fa_off+2]);
  190. if (!off)
  191. continue;
  192. decode(&len, &free, arr + off);
  193. if (long_enough(off, len, size, align)) {
  194. /* Remove it. */
  195. memset(&arr[fa_off], 0, 3);
  196. goto found;
  197. }
  198. }
  199. }
  200. again:
  201. off = arrsize;
  202. decode(&len, &free, arr + off);
  203. while (!free || !long_enough(off, len, size, align)) {
  204. /* Refill free array as we go. */
  205. if (free && coalesced)
  206. add_to_free_array(arr, poolsize, len, off);
  207. off += len;
  208. /* Hit end? */
  209. if (off == poolsize) {
  210. if (!coalesced && try_coalesce(pool, poolsize)) {
  211. coalesced = true;
  212. goto again;
  213. }
  214. return NULL;
  215. }
  216. decode(&len, &free, arr + off);
  217. }
  218. found:
  219. /* We have a free block. Since we walk from front, take far end. */
  220. actual = ((off + len - size) & ~(align - 1));
  221. hdr = actual - encode_len_with_header(off + len - actual);
  222. /* Do we have enough room to split? */
  223. if (hdr - off >= MIN_BLOCK_SIZE) {
  224. encode(hdr - off, true, arr + off);
  225. add_to_free_array(arr, poolsize, hdr - off, off);
  226. } else {
  227. hdr = off;
  228. }
  229. /* Make sure that we are all-zero up to actual, so we can walk back
  230. * and find header. */
  231. memset(arr + hdr, 0, actual - hdr);
  232. /* Create header for allocated block. */
  233. encode(off + len - hdr, false, arr + hdr);
  234. return arr + actual;
  235. }
  236. static unsigned char *to_hdr(void *p)
  237. {
  238. unsigned char *hdr = p;
  239. /* Walk back to find end of header. */
  240. while (!*(--hdr));
  241. assert(*hdr & TERM_BIT);
  242. /* Now walk back to find start of header. */
  243. if (!(*hdr & SINGLE_BYTE)) {
  244. while (!(*(--hdr) & TERM_BIT));
  245. }
  246. return hdr;
  247. }
  248. void tiny_alloc_free(void *pool, unsigned long poolsize, void *freep)
  249. {
  250. unsigned long len;
  251. unsigned char *arr = pool;
  252. unsigned char *hdr;
  253. bool free;
  254. /* Too small to do anything. */
  255. if (poolsize < MIN_BLOCK_SIZE)
  256. return;
  257. hdr = to_hdr(freep);
  258. decode(&len, &free, hdr);
  259. assert(!free);
  260. hdr[0] |= FREE_BIT;
  261. /* If an empty slot, put this in free array. */
  262. add_to_free_array(pool, poolsize, len, hdr - arr);
  263. }
  264. unsigned long tiny_alloc_size(void *pool, unsigned long poolsize, void *p)
  265. {
  266. unsigned char *hdr = to_hdr(p);
  267. unsigned long len, hdrlen;
  268. bool free;
  269. hdrlen = decode(&len, &free, hdr);
  270. return len - hdrlen;
  271. }
  272. /* Useful for gdb breakpoints. */
  273. static bool tiny_check_fail(void)
  274. {
  275. return false;
  276. }
  277. static bool check_decode(const unsigned char *arr, unsigned long len)
  278. {
  279. unsigned int i;
  280. if (len == 0)
  281. return tiny_check_fail();
  282. if (!(arr[0] & TERM_BIT))
  283. return tiny_check_fail();
  284. if (arr[0] & SINGLE_BYTE)
  285. return true;
  286. for (i = 1; i < len; i++) {
  287. if (arr[i] & TERM_BIT)
  288. return true;
  289. }
  290. return tiny_check_fail();
  291. }
  292. bool tiny_alloc_check(void *pool, unsigned long poolsize)
  293. {
  294. unsigned long arrsize = free_array_size(poolsize);
  295. unsigned char *arr = pool;
  296. unsigned long len, off, hdrlen;
  297. unsigned long i, freearr[arrsize], num_freearr = 0;
  298. bool free;
  299. if (poolsize < MIN_BLOCK_SIZE)
  300. return true;
  301. for (i = 0; i + 3 < free_array_size(poolsize); i += 3) {
  302. off = ((unsigned long)arr[i]) << 16
  303. | ((unsigned long)arr[i+1]) << 8
  304. | ((unsigned long)arr[i+2]);
  305. if (!off)
  306. continue;
  307. if (off >= poolsize)
  308. return tiny_check_fail();
  309. freearr[num_freearr++] = off;
  310. }
  311. for (off = arrsize; off < poolsize; off += len) {
  312. /* We should have a valid header. */
  313. if (!check_decode(arr + off, poolsize - off))
  314. return false;
  315. hdrlen = decode(&len, &free, arr + off);
  316. if (off + len > poolsize)
  317. return tiny_check_fail();
  318. if (hdrlen != encode_length(len - MIN_BLOCK_SIZE))
  319. return tiny_check_fail();
  320. for (i = 0; i < num_freearr; i++) {
  321. if (freearr[i] == off) {
  322. if (!free)
  323. return tiny_check_fail();
  324. memmove(&freearr[i], &freearr[i+1],
  325. (num_freearr-- - (i + 1))
  326. * sizeof(freearr[i]));
  327. break;
  328. }
  329. }
  330. }
  331. /* Now we should have found everything in freearr. */
  332. if (num_freearr)
  333. return tiny_check_fail();
  334. /* Now check that sizes are correct. */
  335. for (i = 0; i + 3 < free_array_size(poolsize); i += 3) {
  336. unsigned long fa_off;
  337. off = ((unsigned long)arr[i]) << 16
  338. | ((unsigned long)arr[i+1]) << 8
  339. | ((unsigned long)arr[i+2]);
  340. if (!off)
  341. continue;
  342. decode(&len, &free, arr + off);
  343. /* Would we expect to find this length in this bucket? */
  344. if (len >= MAX_FREE_CACHED_SIZE)
  345. return tiny_check_fail();
  346. for (fa_off = free_array_off(len);
  347. fa_off + 3 < free_array_size(poolsize);
  348. fa_off += free_array_off(MAX_FREE_CACHED_SIZE)) {
  349. if (fa_off == i)
  350. break;
  351. }
  352. if (fa_off != i)
  353. return tiny_check_fail();
  354. }
  355. return true;
  356. }
  357. /* FIXME: Implement. */
  358. void tiny_alloc_visualize(FILE *out, void *pool, unsigned long poolsize)
  359. {
  360. }