|
19 | 19 | #define MSGPACK_RUBY_RMEM_H__ |
20 | 20 |
|
21 | 21 | #include "compat.h" |
22 | | -#include "sysdep.h" |
23 | 22 |
|
24 | 23 | #ifndef MSGPACK_RMEM_PAGE_SIZE |
25 | 24 | #define MSGPACK_RMEM_PAGE_SIZE (4*1024) |
26 | 25 | #endif |
27 | 26 |
|
28 | | -struct msgpack_rmem_t; |
29 | | -typedef struct msgpack_rmem_t msgpack_rmem_t; |
30 | | - |
31 | | -struct msgpack_rmem_chunk_t; |
32 | | -typedef struct msgpack_rmem_chunk_t msgpack_rmem_chunk_t; |
33 | | - |
34 | | -/* |
35 | | - * a chunk contains 32 pages. |
36 | | - * size of each buffer is MSGPACK_RMEM_PAGE_SIZE bytes. |
37 | | - */ |
38 | | -struct msgpack_rmem_chunk_t { |
39 | | - unsigned int mask; |
40 | | - char* pages; |
41 | | -}; |
42 | | - |
43 | | -struct msgpack_rmem_t { |
44 | | - msgpack_rmem_chunk_t head; |
45 | | - msgpack_rmem_chunk_t* array_first; |
46 | | - msgpack_rmem_chunk_t* array_last; |
47 | | - msgpack_rmem_chunk_t* array_end; |
48 | | -}; |
49 | | - |
50 | | -/* assert MSGPACK_RMEM_PAGE_SIZE % sysconf(_SC_PAGE_SIZE) == 0 */ |
51 | | -void msgpack_rmem_init(msgpack_rmem_t* pm); |
52 | | - |
53 | | -void msgpack_rmem_destroy(msgpack_rmem_t* pm); |
54 | | - |
55 | | -void* _msgpack_rmem_alloc2(msgpack_rmem_t* pm); |
56 | | - |
57 | | -#define _msgpack_rmem_chunk_available(c) ((c)->mask != 0) |
58 | | - |
59 | | -static inline void* _msgpack_rmem_chunk_alloc(msgpack_rmem_chunk_t* c) |
| 27 | +/* Fixed-size scratch pages for the buffer and unpacker stack, served straight |
| 28 | + * from xmalloc/xfree so they can be allocated and freed from any Ractor. */ |
| 29 | +static inline void* msgpack_rmem_alloc(void) |
60 | 30 | { |
61 | | - _msgpack_bsp32(pos, c->mask); |
62 | | - (c)->mask &= ~(1 << pos); |
63 | | - return ((char*)(c)->pages) + (pos * (MSGPACK_RMEM_PAGE_SIZE)); |
| 31 | + return xmalloc(MSGPACK_RMEM_PAGE_SIZE); |
64 | 32 | } |
65 | 33 |
|
66 | | -static inline bool _msgpack_rmem_chunk_try_free(msgpack_rmem_chunk_t* c, void* mem) |
| 34 | +static inline void msgpack_rmem_free(void* mem) |
67 | 35 | { |
68 | | - ptrdiff_t pdiff = ((char*)(mem)) - ((char*)(c)->pages); |
69 | | - if(0 <= pdiff && pdiff < MSGPACK_RMEM_PAGE_SIZE * 32) { |
70 | | - size_t pos = pdiff / MSGPACK_RMEM_PAGE_SIZE; |
71 | | - (c)->mask |= (1 << pos); |
72 | | - return true; |
73 | | - } |
74 | | - return false; |
| 36 | + xfree(mem); |
75 | 37 | } |
76 | 38 |
|
77 | | -static inline void* msgpack_rmem_alloc(msgpack_rmem_t* pm) |
78 | | -{ |
79 | | - if(_msgpack_rmem_chunk_available(&pm->head)) { |
80 | | - return _msgpack_rmem_chunk_alloc(&pm->head); |
81 | | - } |
82 | | - return _msgpack_rmem_alloc2(pm); |
83 | | -} |
84 | | - |
85 | | -void _msgpack_rmem_chunk_free(msgpack_rmem_t* pm, msgpack_rmem_chunk_t* c); |
86 | | - |
87 | | -static inline bool msgpack_rmem_free(msgpack_rmem_t* pm, void* mem) |
88 | | -{ |
89 | | - if(_msgpack_rmem_chunk_try_free(&pm->head, mem)) { |
90 | | - return true; |
91 | | - } |
92 | | - |
93 | | - /* search from last */ |
94 | | - msgpack_rmem_chunk_t* c = pm->array_last - 1; |
95 | | - msgpack_rmem_chunk_t* before_first = pm->array_first - 1; |
96 | | - for(; c != before_first; c--) { |
97 | | - if(_msgpack_rmem_chunk_try_free(c, mem)) { |
98 | | - if(c != pm->array_first && c->mask == 0xffffffff) { |
99 | | - _msgpack_rmem_chunk_free(pm, c); |
100 | | - } |
101 | | - return true; |
102 | | - } |
103 | | - } |
104 | | - return false; |
105 | | -} |
106 | | - |
107 | | - |
108 | 39 | #endif |
109 | 40 |
|
0 commit comments