![](/media/cache/profiles/98c39996bf1543e974747a2549b3107c.50x50_q85.png)
JVM test
pool.cpp@5:047542b65d00, 2013-08-24 (annotated)
- Committer:
- lynxeyed_atsu
- Date:
- Sat Aug 24 07:00:51 2013 +0000
- Revision:
- 5:047542b65d00
- Child:
- 7:2a384a077520
KL25Z JVM test
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
lynxeyed_atsu | 5:047542b65d00 | 1 | #include <stdio.h> |
lynxeyed_atsu | 5:047542b65d00 | 2 | #include "pool.h" |
lynxeyed_atsu | 5:047542b65d00 | 3 | |
lynxeyed_atsu | 5:047542b65d00 | 4 | |
lynxeyed_atsu | 5:047542b65d00 | 5 | int pl_buf[pool_size]; |
lynxeyed_atsu | 5:047542b65d00 | 6 | pool_all poal; |
lynxeyed_atsu | 5:047542b65d00 | 7 | |
lynxeyed_atsu | 5:047542b65d00 | 8 | void pool_init(void){ |
lynxeyed_atsu | 5:047542b65d00 | 9 | poal.first_pointer = &pl_buf[0]; |
lynxeyed_atsu | 5:047542b65d00 | 10 | poal.pool_size_all = pool_size; |
lynxeyed_atsu | 5:047542b65d00 | 11 | |
lynxeyed_atsu | 5:047542b65d00 | 12 | poal.available = memory_available; |
lynxeyed_atsu | 5:047542b65d00 | 13 | poal.next_pointer = &pl_buf[(int)((sizeof(pool_all) + (sizeof(int) - 1)) / sizeof(int))]; |
lynxeyed_atsu | 5:047542b65d00 | 14 | memcpy(poal.first_pointer, &poal, sizeof(pool_all)); //todo: change 'pl_buf' pointer |
lynxeyed_atsu | 5:047542b65d00 | 15 | |
lynxeyed_atsu | 5:047542b65d00 | 16 | } |
lynxeyed_atsu | 5:047542b65d00 | 17 | |
lynxeyed_atsu | 5:047542b65d00 | 18 | int *pool_alloc(int size){ |
lynxeyed_atsu | 5:047542b65d00 | 19 | |
lynxeyed_atsu | 5:047542b65d00 | 20 | size = (size + (sizeof(int) - 1)) / sizeof(int); //round int type alignment |
lynxeyed_atsu | 5:047542b65d00 | 21 | |
lynxeyed_atsu | 5:047542b65d00 | 22 | int *pointr; |
lynxeyed_atsu | 5:047542b65d00 | 23 | pool_each poeh; |
lynxeyed_atsu | 5:047542b65d00 | 24 | |
lynxeyed_atsu | 5:047542b65d00 | 25 | poal.pool_size_all = poal.pool_size_all - ((sizeof(pool_each) + (sizeof(int) - 1)) / sizeof(int)) - size; // int alignment |
lynxeyed_atsu | 5:047542b65d00 | 26 | |
lynxeyed_atsu | 5:047542b65d00 | 27 | if(poal.pool_size_all <= 0){ |
lynxeyed_atsu | 5:047542b65d00 | 28 | uart_print("malloc error!\r\n"); |
lynxeyed_atsu | 5:047542b65d00 | 29 | return NULL; |
lynxeyed_atsu | 5:047542b65d00 | 30 | } |
lynxeyed_atsu | 5:047542b65d00 | 31 | |
lynxeyed_atsu | 5:047542b65d00 | 32 | poeh.my_size = size; |
lynxeyed_atsu | 5:047542b65d00 | 33 | #if defined(USE_MAGIC) |
lynxeyed_atsu | 5:047542b65d00 | 34 | poeh.magic_num = magic_value; |
lynxeyed_atsu | 5:047542b65d00 | 35 | #endif |
lynxeyed_atsu | 5:047542b65d00 | 36 | poeh.available = memory_available; |
lynxeyed_atsu | 5:047542b65d00 | 37 | pointr = poal.next_pointer + (int)((sizeof(pool_each) + (sizeof(int) - 1)) / sizeof(int)); |
lynxeyed_atsu | 5:047542b65d00 | 38 | |
lynxeyed_atsu | 5:047542b65d00 | 39 | poeh.this_pointer = &pointr; |
lynxeyed_atsu | 5:047542b65d00 | 40 | |
lynxeyed_atsu | 5:047542b65d00 | 41 | memcpy(poal.next_pointer, &poeh, sizeof(pool_each)); //todo: change 'pl_buf' pointer |
lynxeyed_atsu | 5:047542b65d00 | 42 | poal.next_pointer = pointr + size; |
lynxeyed_atsu | 5:047542b65d00 | 43 | |
lynxeyed_atsu | 5:047542b65d00 | 44 | return pointr; |
lynxeyed_atsu | 5:047542b65d00 | 45 | } |
lynxeyed_atsu | 5:047542b65d00 | 46 | |
lynxeyed_atsu | 5:047542b65d00 | 47 | void pool_free(int *pointr){ |
lynxeyed_atsu | 5:047542b65d00 | 48 | if (pointr == NULL)return; |
lynxeyed_atsu | 5:047542b65d00 | 49 | pool_each poeh; |
lynxeyed_atsu | 5:047542b65d00 | 50 | |
lynxeyed_atsu | 5:047542b65d00 | 51 | pointr = pointr - (int)((sizeof(pool_each) + (sizeof(int) - 1)) / sizeof(int)); // align header |
lynxeyed_atsu | 5:047542b65d00 | 52 | memcpy(&poeh,pointr,sizeof(pool_each)); |
lynxeyed_atsu | 5:047542b65d00 | 53 | #if defined(USE_MAGIC) |
lynxeyed_atsu | 5:047542b65d00 | 54 | if (poeh.magic_num != magic_value){ |
lynxeyed_atsu | 5:047542b65d00 | 55 | uart_print("pointer error\r\n"); |
lynxeyed_atsu | 5:047542b65d00 | 56 | return; |
lynxeyed_atsu | 5:047542b65d00 | 57 | } |
lynxeyed_atsu | 5:047542b65d00 | 58 | #endif |
lynxeyed_atsu | 5:047542b65d00 | 59 | poeh.available = memory_invalid; |
lynxeyed_atsu | 5:047542b65d00 | 60 | memcpy(pointr, &poeh, sizeof(pool_each)); |
lynxeyed_atsu | 5:047542b65d00 | 61 | return; |
lynxeyed_atsu | 5:047542b65d00 | 62 | |
lynxeyed_atsu | 5:047542b65d00 | 63 | } |
lynxeyed_atsu | 5:047542b65d00 | 64 | |
lynxeyed_atsu | 5:047542b65d00 | 65 | int *pool_realloc(int *pointr, int size){ |
lynxeyed_atsu | 5:047542b65d00 | 66 | |
lynxeyed_atsu | 5:047542b65d00 | 67 | int *new_pointr; |
lynxeyed_atsu | 5:047542b65d00 | 68 | |
lynxeyed_atsu | 5:047542b65d00 | 69 | if(pointr == NULL)return NULL; |
lynxeyed_atsu | 5:047542b65d00 | 70 | pool_each poeh; |
lynxeyed_atsu | 5:047542b65d00 | 71 | size = (size + (sizeof(int) - 1)) / sizeof(int); //round int type alignment |
lynxeyed_atsu | 5:047542b65d00 | 72 | |
lynxeyed_atsu | 5:047542b65d00 | 73 | pointr = pointr - (int)((sizeof(pool_each) + (sizeof(int) - 1)) / sizeof(int)); // align header |
lynxeyed_atsu | 5:047542b65d00 | 74 | |
lynxeyed_atsu | 5:047542b65d00 | 75 | memcpy(&poeh,pointr,sizeof(pool_each)); |
lynxeyed_atsu | 5:047542b65d00 | 76 | #if defined(USE_MAGIC) |
lynxeyed_atsu | 5:047542b65d00 | 77 | if (poeh.magic_num != magic_value){ |
lynxeyed_atsu | 5:047542b65d00 | 78 | uart_print("pointer error\r\n"); |
lynxeyed_atsu | 5:047542b65d00 | 79 | return NULL; |
lynxeyed_atsu | 5:047542b65d00 | 80 | } |
lynxeyed_atsu | 5:047542b65d00 | 81 | #endif |
lynxeyed_atsu | 5:047542b65d00 | 82 | if (poeh.my_size >= size){ |
lynxeyed_atsu | 5:047542b65d00 | 83 | |
lynxeyed_atsu | 5:047542b65d00 | 84 | poeh.my_size = size; |
lynxeyed_atsu | 5:047542b65d00 | 85 | memcpy(pointr, &poeh, sizeof(pool_each)); |
lynxeyed_atsu | 5:047542b65d00 | 86 | pointr = pointr + (int)((sizeof(pool_each) + (sizeof(int) - 1)) / sizeof(int)); // align pointer |
lynxeyed_atsu | 5:047542b65d00 | 87 | return pointr; |
lynxeyed_atsu | 5:047542b65d00 | 88 | |
lynxeyed_atsu | 5:047542b65d00 | 89 | } |
lynxeyed_atsu | 5:047542b65d00 | 90 | poeh.available = memory_invalid; |
lynxeyed_atsu | 5:047542b65d00 | 91 | memcpy(pointr, &poeh, sizeof(pool_each)); |
lynxeyed_atsu | 5:047542b65d00 | 92 | |
lynxeyed_atsu | 5:047542b65d00 | 93 | new_pointr = (int *)pool_alloc(sizeof(int) * size); |
lynxeyed_atsu | 5:047542b65d00 | 94 | pointr = pointr + (int)((sizeof(pool_each) + (sizeof(int) - 1)) / sizeof(int)); // align pointer |
lynxeyed_atsu | 5:047542b65d00 | 95 | |
lynxeyed_atsu | 5:047542b65d00 | 96 | memcpy(new_pointr, pointr, sizeof(int) * poeh.my_size); |
lynxeyed_atsu | 5:047542b65d00 | 97 | |
lynxeyed_atsu | 5:047542b65d00 | 98 | return new_pointr; |
lynxeyed_atsu | 5:047542b65d00 | 99 | } |
lynxeyed_atsu | 5:047542b65d00 | 100 | |
lynxeyed_atsu | 5:047542b65d00 | 101 |