This is Unofficial EPICS BASE Doxygen Site
yajl_buf.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2007-2011, Lloyd Hilaiel <lloyd@hilaiel.com>
3  *
4  * Permission to use, copy, modify, and/or distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16 
17 #include <assert.h>
18 #include <stdlib.h>
19 #include <string.h>
20 
21 #include "yajl_buf.h"
22 
23 #define YAJL_BUF_INIT_SIZE 2048
24 
25 struct yajl_buf_t {
26  size_t len;
27  size_t used;
28  unsigned char * data;
30 };
31 
32 static
33 void yajl_buf_ensure_available(yajl_buf buf, size_t want)
34 {
35  size_t need;
36 
37  assert(buf != NULL);
38 
39  /* first call */
40  if (buf->data == NULL) {
41  buf->len = YAJL_BUF_INIT_SIZE;
42  buf->data = (unsigned char *) YA_MALLOC(buf->alloc, buf->len);
43  buf->data[0] = 0;
44  }
45 
46  need = buf->len;
47 
48  while (want >= (need - buf->used)) need <<= 1;
49 
50  if (need != buf->len) {
51  buf->data = (unsigned char *) YA_REALLOC(buf->alloc, buf->data, need);
52  buf->len = need;
53  }
54 }
55 
57 {
58  yajl_buf b = YA_MALLOC(alloc, sizeof(struct yajl_buf_t));
59  if (b == NULL) {
60  return NULL;
61  }
62 
63  memset((void *) b, 0, sizeof(struct yajl_buf_t));
64  b->alloc = alloc;
65  return b;
66 }
67 
69 {
70  assert(buf != NULL);
71  if (buf->data) YA_FREE(buf->alloc, buf->data);
72  YA_FREE(buf->alloc, buf);
73 }
74 
75 void yajl_buf_append(yajl_buf buf, const void * data, size_t len)
76 {
77  yajl_buf_ensure_available(buf, len);
78  if (len > 0) {
79  assert(data != NULL);
80  memcpy(buf->data + buf->used, data, len);
81  buf->used += len;
82  buf->data[buf->used] = 0;
83  }
84 }
85 
87 {
88  buf->used = 0;
89  if (buf->data) buf->data[buf->used] = 0;
90 }
91 
92 const unsigned char * yajl_buf_data(yajl_buf buf)
93 {
94  return buf->data;
95 }
96 
98 {
99  return buf->used;
100 }
101 
102 void
104 {
105  assert(len <= buf->used);
106  buf->used = len;
107 }
#define YA_MALLOC(afs, sz)
Definition: yajl_alloc.h:32
#define assert(exp)
Declare that a condition should be true.
Definition: epicsAssert.h:70
#define YA_FREE(afs, ptr)
Definition: yajl_alloc.h:33
#define YAJL_BUF_INIT_SIZE
Definition: yajl_buf.c:23
yajl_buf yajl_buf_alloc(yajl_alloc_funcs *alloc)
Definition: yajl_buf.c:56
#define NULL
Definition: catime.c:38
unsigned char * data
Definition: yajl_buf.c:28
size_t yajl_buf_len(yajl_buf buf)
Definition: yajl_buf.c:97
void yajl_buf_append(yajl_buf buf, const void *data, size_t len)
Definition: yajl_buf.c:75
void yajl_buf_truncate(yajl_buf buf, size_t len)
Definition: yajl_buf.c:103
#define YA_REALLOC(afs, ptr, sz)
Definition: yajl_alloc.h:34
void yajl_buf_clear(yajl_buf buf)
Definition: yajl_buf.c:86
void yajl_buf_free(yajl_buf buf)
Definition: yajl_buf.c:68
size_t len
Definition: yajl_buf.c:26
yajl_alloc_funcs * alloc
Definition: yajl_buf.c:29
const unsigned char * yajl_buf_data(yajl_buf buf)
Definition: yajl_buf.c:92
size_t used
Definition: yajl_buf.c:27