1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
|
/*
* CBOR for Ruby
*
* Copyright (C) 2013 Carsten Bormann
*
* Licensed under the Apache License, Version 2.0 (the "License").
*
* Based on:
***********/
/*
* MessagePack for Ruby
*
* Copyright (C) 2008-2013 Sadayuki Furuhashi
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef MSGPACK_RUBY_RMEM_H__
#define MSGPACK_RUBY_RMEM_H__
#include "compat.h"
#include "sysdep.h"
#ifndef MSGPACK_RMEM_PAGE_SIZE
#define MSGPACK_RMEM_PAGE_SIZE (4*1024)
#endif
struct msgpack_rmem_t;
typedef struct msgpack_rmem_t msgpack_rmem_t;
struct msgpack_rmem_chunk_t;
typedef struct msgpack_rmem_chunk_t msgpack_rmem_chunk_t;
/*
* a chunk contains 32 pages.
* size of each buffer is MSGPACK_RMEM_PAGE_SIZE bytes.
*/
struct msgpack_rmem_chunk_t {
unsigned int mask;
char* pages;
};
struct msgpack_rmem_t {
msgpack_rmem_chunk_t head;
msgpack_rmem_chunk_t* array_first;
msgpack_rmem_chunk_t* array_last;
msgpack_rmem_chunk_t* array_end;
};
/* assert MSGPACK_RMEM_PAGE_SIZE % sysconf(_SC_PAGE_SIZE) == 0 */
void msgpack_rmem_init(msgpack_rmem_t* pm);
void msgpack_rmem_destroy(msgpack_rmem_t* pm);
void* _msgpack_rmem_alloc2(msgpack_rmem_t* pm);
#define _msgpack_rmem_chunk_available(c) ((c)->mask != 0)
static inline void* _msgpack_rmem_chunk_alloc(msgpack_rmem_chunk_t* c)
{
_msgpack_bsp32(pos, c->mask);
(c)->mask &= ~(1 << pos);
return ((char*)(c)->pages) + (pos * (MSGPACK_RMEM_PAGE_SIZE));
}
static inline bool _msgpack_rmem_chunk_try_free(msgpack_rmem_chunk_t* c, void* mem)
{
ptrdiff_t pdiff = ((char*)(mem)) - ((char*)(c)->pages);
if(0 <= pdiff && pdiff < MSGPACK_RMEM_PAGE_SIZE * 32) {
size_t pos = pdiff / MSGPACK_RMEM_PAGE_SIZE;
(c)->mask |= (1 << pos);
return true;
}
return false;
}
static inline void* msgpack_rmem_alloc(msgpack_rmem_t* pm)
{
if(_msgpack_rmem_chunk_available(&pm->head)) {
return _msgpack_rmem_chunk_alloc(&pm->head);
}
return _msgpack_rmem_alloc2(pm);
}
void _msgpack_rmem_chunk_free(msgpack_rmem_t* pm, msgpack_rmem_chunk_t* c);
static inline bool msgpack_rmem_free(msgpack_rmem_t* pm, void* mem)
{
if(_msgpack_rmem_chunk_try_free(&pm->head, mem)) {
return true;
}
/* search from last */
msgpack_rmem_chunk_t* c = pm->array_last - 1;
msgpack_rmem_chunk_t* before_first = pm->array_first - 1;
for(; c != before_first; c--) {
if(_msgpack_rmem_chunk_try_free(c, mem)) {
if(c != pm->array_first && c->mask == 0xffffffff) {
_msgpack_rmem_chunk_free(pm, c);
}
return true;
}
}
return false;
}
#endif
|