Hideaki Tai / msgpack-embedded

Dependents:   hello_message_pack

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers sbuffer.h Source File

sbuffer.h

00001 /*
00002  * MessagePack for C simple buffer implementation
00003  *
00004  * Copyright (C) 2008-2009 FURUHASHI Sadayuki
00005  *
00006  *    Distributed under the Boost Software License, Version 1.0.
00007  *    (See accompanying file LICENSE_1_0.txt or copy at
00008  *    http://www.boost.org/LICENSE_1_0.txt)
00009  */
00010 #ifndef MSGPACK_SBUFFER_H
00011 #define MSGPACK_SBUFFER_H
00012 
00013 #include <stdlib.h>
00014 #include <string.h>
00015 
00016 #ifdef __cplusplus
00017 extern "C" {
00018 #endif
00019 
00020 
00021 /**
00022  * @defgroup msgpack_sbuffer Simple buffer
00023  * @ingroup msgpack_buffer
00024  * @{
00025  */
00026 
00027 typedef struct msgpack_sbuffer {
00028     size_t size;
00029     char* data;
00030     size_t alloc;
00031 } msgpack_sbuffer;
00032 
00033 static inline void msgpack_sbuffer_init(msgpack_sbuffer* sbuf)
00034 {
00035     memset(sbuf, 0, sizeof(msgpack_sbuffer));
00036 }
00037 
00038 static inline void msgpack_sbuffer_destroy(msgpack_sbuffer* sbuf)
00039 {
00040     free(sbuf->data);
00041 }
00042 
00043 static inline msgpack_sbuffer* msgpack_sbuffer_new(void)
00044 {
00045     return (msgpack_sbuffer*)calloc(1, sizeof(msgpack_sbuffer));
00046 }
00047 
00048 static inline void msgpack_sbuffer_free(msgpack_sbuffer* sbuf)
00049 {
00050     if(sbuf == NULL) { return; }
00051     msgpack_sbuffer_destroy(sbuf);
00052     free(sbuf);
00053 }
00054 
00055 #ifndef MSGPACK_SBUFFER_INIT_SIZE
00056     #if defined(__MBED__)
00057         #define MSGPACK_SBUFFER_INIT_SIZE 512
00058     #elif defined(__AVR__)
00059         #define MSGPACK_SBUFFER_INIT_SIZE 128
00060     #else
00061         #define MSGPACK_SBUFFER_INIT_SIZE 8192
00062     #endif
00063 #endif
00064 
00065 static inline int msgpack_sbuffer_write(void* data, const char* buf, size_t len)
00066 {
00067     msgpack_sbuffer* sbuf = (msgpack_sbuffer*)data;
00068 
00069     if(sbuf->alloc - sbuf->size < len) {
00070         void* tmp;
00071         size_t nsize = (sbuf->alloc) ?
00072                 sbuf->alloc * 2 : MSGPACK_SBUFFER_INIT_SIZE;
00073 
00074         while(nsize < sbuf->size + len) {
00075             size_t tmp_nsize = nsize * 2;
00076             if (tmp_nsize <= nsize) {
00077                 nsize = sbuf->size + len;
00078                 break;
00079             }
00080             nsize = tmp_nsize;
00081         }
00082 
00083         tmp = realloc(sbuf->data, nsize);
00084         if(!tmp) { return -1; }
00085 
00086         sbuf->data = (char*)tmp;
00087         sbuf->alloc = nsize;
00088     }
00089 
00090     memcpy(sbuf->data + sbuf->size, buf, len);
00091     sbuf->size += len;
00092     return 0;
00093 }
00094 
00095 static inline char* msgpack_sbuffer_release(msgpack_sbuffer* sbuf)
00096 {
00097     char* tmp = sbuf->data;
00098     sbuf->size = 0;
00099     sbuf->data = NULL;
00100     sbuf->alloc = 0;
00101     return tmp;
00102 }
00103 
00104 static inline void msgpack_sbuffer_clear(msgpack_sbuffer* sbuf)
00105 {
00106     sbuf->size = 0;
00107 }
00108 
00109 /** @} */
00110 
00111 
00112 #ifdef __cplusplus
00113 }
00114 #endif
00115 
00116 #endif /* msgpack/sbuffer.h */