c: msgpack_sbuffer; cpp: msgpack::sbuffer

This commit is contained in:
frsyuki 2009-03-01 00:59:15 +09:00
parent c60b6be548
commit 4f4fa39cd5
4 changed files with 73 additions and 39 deletions

View File

@ -21,14 +21,18 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#ifndef MSGPACK_SBUFFER_INIT_SIZE
#define MSGPACK_SBUFFER_INIT_SIZE 2048
#endif
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
typedef struct { typedef struct msgpack_sbuffer {
char* ptr;
size_t size; size_t size;
size_t capacity; char* data;
size_t alloc;
} msgpack_sbuffer; } msgpack_sbuffer;
static inline void msgpack_sbuffer_init(msgpack_sbuffer* sbuf) static inline void msgpack_sbuffer_init(msgpack_sbuffer* sbuf)
@ -38,27 +42,40 @@ static inline void msgpack_sbuffer_init(msgpack_sbuffer* sbuf)
static inline void msgpack_sbuffer_destroy(msgpack_sbuffer* sbuf) static inline void msgpack_sbuffer_destroy(msgpack_sbuffer* sbuf)
{ {
free(sbuf->ptr); free(sbuf->data);
} }
static inline int msgpack_sbuffer_write(void* data, const char* buf, unsigned int len) static inline int msgpack_sbuffer_write(void* data, const char* buf, unsigned int len)
{ {
msgpack_sbuffer* sbuf = (msgpack_sbuffer*)data; msgpack_sbuffer* sbuf = (msgpack_sbuffer*)data;
if(sbuf->capacity - sbuf->size < len) {
size_t nsize = (sbuf->capacity ? sbuf->capacity*2 : 2048); if(sbuf->alloc - sbuf->size < len) {
size_t nsize = (sbuf->alloc) ?
sbuf->alloc * 2 : MSGPACK_SBUFFER_INIT_SIZE;
while(nsize < sbuf->size + len) { nsize *= 2; } while(nsize < sbuf->size + len) { nsize *= 2; }
void* tmp = realloc(sbuf->ptr, nsize); void* tmp = realloc(sbuf->data, nsize);
if(!tmp) { return -1; } if(!tmp) { return -1; }
sbuf->ptr = (char*)tmp; sbuf->data = (char*)tmp;
sbuf->capacity = nsize; sbuf->alloc = nsize;
} }
memcpy(sbuf->ptr + sbuf->size, buf, len);
memcpy(sbuf->data + sbuf->size, buf, len);
sbuf->size += len; sbuf->size += len;
return 0; return 0;
} }
static inline char* msgpack_sbuffer_release(msgpack_sbuffer* sbuf)
{
char* tmp = sbuf->data;
sbuf->data = NULL;
sbuf->size = 0;
sbuf->alloc = NULL;
return tmp;
}
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@ -18,62 +18,80 @@
#ifndef MSGPACK_SBUFFER_HPP__ #ifndef MSGPACK_SBUFFER_HPP__
#define MSGPACK_SBUFFER_HPP__ #define MSGPACK_SBUFFER_HPP__
#include <string.h> #include "msgpack/sbuffer.h"
#include <stdlib.h>
#include <stdexcept> #include <stdexcept>
namespace msgpack { namespace msgpack {
class sbuffer { class sbuffer : public msgpack_sbuffer {
public: public:
sbuffer() : m_capacity(0), m_size(0), m_ptr(NULL) { } sbuffer(size_t initsz = MSGPACK_SBUFFER_INIT_SIZE)
{
msgpack_sbuffer* sbuf = static_cast<msgpack_sbuffer*>(this);
sbuf->data = (char*)::malloc(initsz);
if(!sbuf->data) {
throw std::bad_alloc();
}
sbuf->size = 0;
sbuf->alloc = initsz;
}
~sbuffer() ~sbuffer()
{ {
free(m_ptr); msgpack_sbuffer* sbuf = static_cast<msgpack_sbuffer*>(this);
::free(sbuf->data);
} }
public: public:
void write(const char* buf, size_t len) void write(const char* buf, unsigned int len)
{ {
if(m_capacity - m_size < len) { msgpack_sbuffer* sbuf = static_cast<msgpack_sbuffer*>(this);
size_t nsize = (m_capacity ? m_capacity*2 : 2048); if(sbuf->alloc - sbuf->size < len) {
while(nsize < m_size + len) { nsize *= 2; } expand_buffer(len);
char* tmp = (char*)realloc(m_ptr, nsize);
if(!tmp) { throw std::bad_alloc(); }
m_ptr = tmp;
m_capacity = nsize;
} }
memcpy(m_ptr + m_size, buf, len); memcpy(sbuf->data + sbuf->size, buf, len);
m_size += len; sbuf->size += len;
} }
char* data() char* data()
{ {
return m_ptr; msgpack_sbuffer* sbuf = static_cast<msgpack_sbuffer*>(this);
return sbuf->data;
} }
size_t size() const size_t size() const
{ {
return m_size; const msgpack_sbuffer* sbuf = static_cast<const msgpack_sbuffer*>(this);
return sbuf->size;
} }
char* release() char* release()
{ {
char* tmp = m_ptr; msgpack_sbuffer* sbuf = static_cast<msgpack_sbuffer*>(this);
m_capacity = 0; return msgpack_sbuffer_release(sbuf);
m_size = 0;
m_ptr = NULL;
return tmp;
} }
private: private:
size_t m_capacity; void expand_buffer(size_t len)
size_t m_size; {
char* m_ptr; msgpack_sbuffer* sbuf = static_cast<msgpack_sbuffer*>(this);
size_t nsize = (sbuf->alloc) ?
sbuf->alloc * 2 : MSGPACK_SBUFFER_INIT_SIZE;
while(nsize < sbuf->size + len) { nsize *= 2; }
void* tmp = realloc(sbuf->data, nsize);
if(!tmp) {
throw std::bad_alloc();
}
sbuf->data = (char*)tmp;
sbuf->alloc = nsize;
}
private: private:
sbuffer(const sbuffer&); sbuffer(const sbuffer&);

View File

@ -99,4 +99,3 @@ T* zone::allocate(<%=(1..i).map{|j|"A#{j} a#{j}"}.join(', ')%>)
#endif /* msgpack/zone.hpp */ #endif /* msgpack/zone.hpp */
// vim: ft=cpp ts=4 sw=4 softtabstop=4 noexpandtab smarttab

View File

@ -23,7 +23,7 @@ int main(void)
msgpack_zone_init(&mempool, 2048); msgpack_zone_init(&mempool, 2048);
msgpack_object deserialized; msgpack_object deserialized;
msgpack_unpack(sbuf.ptr, sbuf.size, NULL, &mempool, &deserialized); msgpack_unpack(sbuf.data, sbuf.size, NULL, &mempool, &deserialized);
/* print the deserialized object. */ /* print the deserialized object. */
msgpack_object_print(stdout, deserialized); msgpack_object_print(stdout, deserialized);