messagepack implementation for embedded systems (mbed / arduino)
Dependents: hello_message_pack
msgpack-c & msgpack-c++ https://github.com/msgpack/msgpack-c implementation for embedded systems (mbed / Arduino)
include/zone.h@4:bd0c06dd6e92, 2016-02-22 (annotated)
- Committer:
- hideakitai
- Date:
- Mon Feb 22 01:43:48 2016 +0000
- Revision:
- 4:bd0c06dd6e92
- Parent:
- 3:a56553e46a9a
fix throw error
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hideakitai | 0:3f9dbf1e2cb0 | 1 | /* |
hideakitai | 0:3f9dbf1e2cb0 | 2 | * MessagePack for C memory pool implementation |
hideakitai | 0:3f9dbf1e2cb0 | 3 | * |
hideakitai | 0:3f9dbf1e2cb0 | 4 | * Copyright (C) 2008-2010 FURUHASHI Sadayuki |
hideakitai | 0:3f9dbf1e2cb0 | 5 | * |
hideakitai | 0:3f9dbf1e2cb0 | 6 | * Distributed under the Boost Software License, Version 1.0. |
hideakitai | 0:3f9dbf1e2cb0 | 7 | * (See accompanying file LICENSE_1_0.txt or copy at |
hideakitai | 0:3f9dbf1e2cb0 | 8 | * http://www.boost.org/LICENSE_1_0.txt) |
hideakitai | 0:3f9dbf1e2cb0 | 9 | */ |
hideakitai | 0:3f9dbf1e2cb0 | 10 | #ifndef MSGPACK_ZONE_H |
hideakitai | 0:3f9dbf1e2cb0 | 11 | #define MSGPACK_ZONE_H |
hideakitai | 0:3f9dbf1e2cb0 | 12 | |
hideakitai | 0:3f9dbf1e2cb0 | 13 | #include "sysdep.h" |
hideakitai | 0:3f9dbf1e2cb0 | 14 | |
hideakitai | 0:3f9dbf1e2cb0 | 15 | #ifdef __cplusplus |
hideakitai | 0:3f9dbf1e2cb0 | 16 | extern "C" { |
hideakitai | 0:3f9dbf1e2cb0 | 17 | #endif |
hideakitai | 0:3f9dbf1e2cb0 | 18 | |
hideakitai | 0:3f9dbf1e2cb0 | 19 | |
hideakitai | 0:3f9dbf1e2cb0 | 20 | /** |
hideakitai | 0:3f9dbf1e2cb0 | 21 | * @defgroup msgpack_zone Memory zone |
hideakitai | 0:3f9dbf1e2cb0 | 22 | * @ingroup msgpack |
hideakitai | 0:3f9dbf1e2cb0 | 23 | * @{ |
hideakitai | 0:3f9dbf1e2cb0 | 24 | */ |
hideakitai | 0:3f9dbf1e2cb0 | 25 | |
hideakitai | 0:3f9dbf1e2cb0 | 26 | typedef struct msgpack_zone_finalizer { |
hideakitai | 0:3f9dbf1e2cb0 | 27 | void (*func)(void* data); |
hideakitai | 0:3f9dbf1e2cb0 | 28 | void* data; |
hideakitai | 0:3f9dbf1e2cb0 | 29 | } msgpack_zone_finalizer; |
hideakitai | 0:3f9dbf1e2cb0 | 30 | |
hideakitai | 0:3f9dbf1e2cb0 | 31 | typedef struct msgpack_zone_finalizer_array { |
hideakitai | 0:3f9dbf1e2cb0 | 32 | msgpack_zone_finalizer* tail; |
hideakitai | 0:3f9dbf1e2cb0 | 33 | msgpack_zone_finalizer* end; |
hideakitai | 0:3f9dbf1e2cb0 | 34 | msgpack_zone_finalizer* array; |
hideakitai | 0:3f9dbf1e2cb0 | 35 | } msgpack_zone_finalizer_array; |
hideakitai | 0:3f9dbf1e2cb0 | 36 | |
hideakitai | 0:3f9dbf1e2cb0 | 37 | struct msgpack_zone_chunk; |
hideakitai | 0:3f9dbf1e2cb0 | 38 | typedef struct msgpack_zone_chunk msgpack_zone_chunk; |
hideakitai | 0:3f9dbf1e2cb0 | 39 | |
hideakitai | 0:3f9dbf1e2cb0 | 40 | typedef struct msgpack_zone_chunk_list { |
hideakitai | 0:3f9dbf1e2cb0 | 41 | size_t free; |
hideakitai | 0:3f9dbf1e2cb0 | 42 | char* ptr; |
hideakitai | 0:3f9dbf1e2cb0 | 43 | msgpack_zone_chunk* head; |
hideakitai | 0:3f9dbf1e2cb0 | 44 | } msgpack_zone_chunk_list; |
hideakitai | 0:3f9dbf1e2cb0 | 45 | |
hideakitai | 0:3f9dbf1e2cb0 | 46 | typedef struct msgpack_zone { |
hideakitai | 0:3f9dbf1e2cb0 | 47 | msgpack_zone_chunk_list chunk_list; |
hideakitai | 0:3f9dbf1e2cb0 | 48 | msgpack_zone_finalizer_array finalizer_array; |
hideakitai | 0:3f9dbf1e2cb0 | 49 | size_t chunk_size; |
hideakitai | 0:3f9dbf1e2cb0 | 50 | } msgpack_zone; |
hideakitai | 0:3f9dbf1e2cb0 | 51 | |
hideakitai | 0:3f9dbf1e2cb0 | 52 | #ifndef MSGPACK_ZONE_CHUNK_SIZE |
hideakitai | 0:3f9dbf1e2cb0 | 53 | #if defined(__MBED__) |
hideakitai | 3:a56553e46a9a | 54 | #define MSGPACK_ZONE_CHUNK_SIZE 512 |
hideakitai | 0:3f9dbf1e2cb0 | 55 | #elif defined(__AVR__) |
hideakitai | 0:3f9dbf1e2cb0 | 56 | #define MSGPACK_ZONE_CHUNK_SIZE 128 |
hideakitai | 0:3f9dbf1e2cb0 | 57 | #else |
hideakitai | 0:3f9dbf1e2cb0 | 58 | #define MSGPACK_ZONE_CHUNK_SIZE 8192 |
hideakitai | 0:3f9dbf1e2cb0 | 59 | #endif |
hideakitai | 0:3f9dbf1e2cb0 | 60 | #endif |
hideakitai | 0:3f9dbf1e2cb0 | 61 | |
hideakitai | 0:3f9dbf1e2cb0 | 62 | MSGPACK_DLLEXPORT |
hideakitai | 0:3f9dbf1e2cb0 | 63 | bool msgpack_zone_init(msgpack_zone* zone, size_t chunk_size); |
hideakitai | 0:3f9dbf1e2cb0 | 64 | MSGPACK_DLLEXPORT |
hideakitai | 0:3f9dbf1e2cb0 | 65 | void msgpack_zone_destroy(msgpack_zone* zone); |
hideakitai | 0:3f9dbf1e2cb0 | 66 | |
hideakitai | 0:3f9dbf1e2cb0 | 67 | MSGPACK_DLLEXPORT |
hideakitai | 0:3f9dbf1e2cb0 | 68 | msgpack_zone* msgpack_zone_new(size_t chunk_size); |
hideakitai | 0:3f9dbf1e2cb0 | 69 | MSGPACK_DLLEXPORT |
hideakitai | 0:3f9dbf1e2cb0 | 70 | void msgpack_zone_free(msgpack_zone* zone); |
hideakitai | 0:3f9dbf1e2cb0 | 71 | |
hideakitai | 0:3f9dbf1e2cb0 | 72 | static inline void* msgpack_zone_malloc(msgpack_zone* zone, size_t size); |
hideakitai | 0:3f9dbf1e2cb0 | 73 | static inline void* msgpack_zone_malloc_no_align(msgpack_zone* zone, size_t size); |
hideakitai | 0:3f9dbf1e2cb0 | 74 | |
hideakitai | 0:3f9dbf1e2cb0 | 75 | static inline bool msgpack_zone_push_finalizer(msgpack_zone* zone, |
hideakitai | 0:3f9dbf1e2cb0 | 76 | void (*func)(void* data), void* data); |
hideakitai | 0:3f9dbf1e2cb0 | 77 | |
hideakitai | 0:3f9dbf1e2cb0 | 78 | static inline void msgpack_zone_swap(msgpack_zone* a, msgpack_zone* b); |
hideakitai | 0:3f9dbf1e2cb0 | 79 | |
hideakitai | 0:3f9dbf1e2cb0 | 80 | MSGPACK_DLLEXPORT |
hideakitai | 0:3f9dbf1e2cb0 | 81 | bool msgpack_zone_is_empty(msgpack_zone* zone); |
hideakitai | 0:3f9dbf1e2cb0 | 82 | |
hideakitai | 0:3f9dbf1e2cb0 | 83 | MSGPACK_DLLEXPORT |
hideakitai | 0:3f9dbf1e2cb0 | 84 | void msgpack_zone_clear(msgpack_zone* zone); |
hideakitai | 0:3f9dbf1e2cb0 | 85 | |
hideakitai | 0:3f9dbf1e2cb0 | 86 | /** @} */ |
hideakitai | 0:3f9dbf1e2cb0 | 87 | |
hideakitai | 0:3f9dbf1e2cb0 | 88 | |
hideakitai | 0:3f9dbf1e2cb0 | 89 | #ifndef MSGPACK_ZONE_ALIGN |
hideakitai | 0:3f9dbf1e2cb0 | 90 | #define MSGPACK_ZONE_ALIGN sizeof(void*) |
hideakitai | 0:3f9dbf1e2cb0 | 91 | #endif |
hideakitai | 0:3f9dbf1e2cb0 | 92 | |
hideakitai | 0:3f9dbf1e2cb0 | 93 | MSGPACK_DLLEXPORT |
hideakitai | 0:3f9dbf1e2cb0 | 94 | void* msgpack_zone_malloc_expand(msgpack_zone* zone, size_t size); |
hideakitai | 0:3f9dbf1e2cb0 | 95 | |
hideakitai | 0:3f9dbf1e2cb0 | 96 | static inline void* msgpack_zone_malloc_no_align(msgpack_zone* zone, size_t size) |
hideakitai | 0:3f9dbf1e2cb0 | 97 | { |
hideakitai | 0:3f9dbf1e2cb0 | 98 | char* ptr; |
hideakitai | 0:3f9dbf1e2cb0 | 99 | msgpack_zone_chunk_list* cl = &zone->chunk_list; |
hideakitai | 0:3f9dbf1e2cb0 | 100 | |
hideakitai | 0:3f9dbf1e2cb0 | 101 | if(zone->chunk_list.free < size) { |
hideakitai | 0:3f9dbf1e2cb0 | 102 | return msgpack_zone_malloc_expand(zone, size); |
hideakitai | 0:3f9dbf1e2cb0 | 103 | } |
hideakitai | 0:3f9dbf1e2cb0 | 104 | |
hideakitai | 0:3f9dbf1e2cb0 | 105 | ptr = cl->ptr; |
hideakitai | 0:3f9dbf1e2cb0 | 106 | cl->free -= size; |
hideakitai | 0:3f9dbf1e2cb0 | 107 | cl->ptr += size; |
hideakitai | 0:3f9dbf1e2cb0 | 108 | |
hideakitai | 0:3f9dbf1e2cb0 | 109 | return ptr; |
hideakitai | 0:3f9dbf1e2cb0 | 110 | } |
hideakitai | 0:3f9dbf1e2cb0 | 111 | |
hideakitai | 0:3f9dbf1e2cb0 | 112 | static inline void* msgpack_zone_malloc(msgpack_zone* zone, size_t size) |
hideakitai | 0:3f9dbf1e2cb0 | 113 | { |
hideakitai | 0:3f9dbf1e2cb0 | 114 | char* aligned = |
hideakitai | 0:3f9dbf1e2cb0 | 115 | (char*)( |
hideakitai | 0:3f9dbf1e2cb0 | 116 | (size_t)( |
hideakitai | 0:3f9dbf1e2cb0 | 117 | zone->chunk_list.ptr + (MSGPACK_ZONE_ALIGN - 1) |
hideakitai | 0:3f9dbf1e2cb0 | 118 | ) / MSGPACK_ZONE_ALIGN * MSGPACK_ZONE_ALIGN |
hideakitai | 0:3f9dbf1e2cb0 | 119 | ); |
hideakitai | 0:3f9dbf1e2cb0 | 120 | size_t adjusted_size = size + (aligned - zone->chunk_list.ptr); |
hideakitai | 0:3f9dbf1e2cb0 | 121 | if(zone->chunk_list.free >= adjusted_size) { |
hideakitai | 0:3f9dbf1e2cb0 | 122 | zone->chunk_list.free -= adjusted_size; |
hideakitai | 0:3f9dbf1e2cb0 | 123 | zone->chunk_list.ptr += adjusted_size; |
hideakitai | 0:3f9dbf1e2cb0 | 124 | return aligned; |
hideakitai | 0:3f9dbf1e2cb0 | 125 | } |
hideakitai | 0:3f9dbf1e2cb0 | 126 | { |
hideakitai | 0:3f9dbf1e2cb0 | 127 | void* ptr = msgpack_zone_malloc_expand(zone, size + (MSGPACK_ZONE_ALIGN - 1)); |
hideakitai | 0:3f9dbf1e2cb0 | 128 | if (ptr) { |
hideakitai | 0:3f9dbf1e2cb0 | 129 | return (char*)((size_t)(ptr) / MSGPACK_ZONE_ALIGN * MSGPACK_ZONE_ALIGN); |
hideakitai | 0:3f9dbf1e2cb0 | 130 | } |
hideakitai | 0:3f9dbf1e2cb0 | 131 | } |
hideakitai | 0:3f9dbf1e2cb0 | 132 | return NULL; |
hideakitai | 0:3f9dbf1e2cb0 | 133 | } |
hideakitai | 0:3f9dbf1e2cb0 | 134 | |
hideakitai | 0:3f9dbf1e2cb0 | 135 | |
hideakitai | 0:3f9dbf1e2cb0 | 136 | bool msgpack_zone_push_finalizer_expand(msgpack_zone* zone, |
hideakitai | 0:3f9dbf1e2cb0 | 137 | void (*func)(void* data), void* data); |
hideakitai | 0:3f9dbf1e2cb0 | 138 | |
hideakitai | 0:3f9dbf1e2cb0 | 139 | static inline bool msgpack_zone_push_finalizer(msgpack_zone* zone, |
hideakitai | 0:3f9dbf1e2cb0 | 140 | void (*func)(void* data), void* data) |
hideakitai | 0:3f9dbf1e2cb0 | 141 | { |
hideakitai | 0:3f9dbf1e2cb0 | 142 | msgpack_zone_finalizer_array* const fa = &zone->finalizer_array; |
hideakitai | 0:3f9dbf1e2cb0 | 143 | msgpack_zone_finalizer* fin = fa->tail; |
hideakitai | 0:3f9dbf1e2cb0 | 144 | |
hideakitai | 0:3f9dbf1e2cb0 | 145 | if(fin == fa->end) { |
hideakitai | 0:3f9dbf1e2cb0 | 146 | return msgpack_zone_push_finalizer_expand(zone, func, data); |
hideakitai | 0:3f9dbf1e2cb0 | 147 | } |
hideakitai | 0:3f9dbf1e2cb0 | 148 | |
hideakitai | 0:3f9dbf1e2cb0 | 149 | fin->func = func; |
hideakitai | 0:3f9dbf1e2cb0 | 150 | fin->data = data; |
hideakitai | 0:3f9dbf1e2cb0 | 151 | |
hideakitai | 0:3f9dbf1e2cb0 | 152 | ++fa->tail; |
hideakitai | 0:3f9dbf1e2cb0 | 153 | |
hideakitai | 0:3f9dbf1e2cb0 | 154 | return true; |
hideakitai | 0:3f9dbf1e2cb0 | 155 | } |
hideakitai | 0:3f9dbf1e2cb0 | 156 | |
hideakitai | 0:3f9dbf1e2cb0 | 157 | static inline void msgpack_zone_swap(msgpack_zone* a, msgpack_zone* b) |
hideakitai | 0:3f9dbf1e2cb0 | 158 | { |
hideakitai | 0:3f9dbf1e2cb0 | 159 | msgpack_zone tmp = *a; |
hideakitai | 0:3f9dbf1e2cb0 | 160 | *a = *b; |
hideakitai | 0:3f9dbf1e2cb0 | 161 | *b = tmp; |
hideakitai | 0:3f9dbf1e2cb0 | 162 | } |
hideakitai | 0:3f9dbf1e2cb0 | 163 | |
hideakitai | 0:3f9dbf1e2cb0 | 164 | |
hideakitai | 0:3f9dbf1e2cb0 | 165 | #ifdef __cplusplus |
hideakitai | 0:3f9dbf1e2cb0 | 166 | } |
hideakitai | 0:3f9dbf1e2cb0 | 167 | #endif |
hideakitai | 0:3f9dbf1e2cb0 | 168 | |
hideakitai | 0:3f9dbf1e2cb0 | 169 | #endif /* msgpack/zone.h */ |