Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: f2710fd703
Fetching contributors…

Cannot retrieve contributors at this time

file 166 lines (132 sloc) 4.344 kb
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 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
/**
Copyright (c) 2009 Notion <http://notion.se/>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
**/
#include "buffer.h"
#include "mpool.h"
#include <stdarg.h>


#define ADDROF(a, i, size) ((void **)((a)->start + ((size) * (i))))


NT_OBJ(nt_buffer_t, nt_buffer_new(size_t size, size_t growextra),
{/* constructor: */
  if (size == 0)
size = growextra;
if (size == 0)
    size = 1;
  size = NT_ALIGN_M(size); // min size is machine alignment
if ( (self->start = (byte_t *)nt_malloc(size)) == NULL ) {
    return NULL; // ENOMEM
  }
self->end = self->start + size;
self->ptr = self->start;
  self->growextra = growextra;
},
{/* destructor: */
  if (self->start && nt_buffer_size(self)) {
    nt_free(self->start, nt_buffer_size(self));
  }
})


bool nt_buffer_grow(nt_buffer_t *self, size_t length) {
byte_t *new_start;
size_t new_size;

if (length <= nt_buffer_available(self))
return true;

  if (
#ifdef NT_BUFFER_GROW_MAX
   length > NT_BUFFER_GROW_MAX ||
   #endif
self->start == NULL) {
return false;
  }
  
new_size = NT_ALIGN_M(nt_buffer_occupied(self) + length + self->growextra);
#ifdef NT_BUFFER_GROW_MAX
if (new_size > NT_BUFFER_GROW_MAX)
return false;
#endif

if ((new_start = nt_realloc(self->start, nt_buffer_size(self), new_size)) == NULL)
    err(1, NULL);
  
self->ptr = new_start + nt_buffer_occupied(self);
self->start = new_start;
self->end = new_start + new_size;

return true;
}


bool nt_buffer_append(nt_buffer_t *self, const byte_t *what, size_t length) {
if (length > nt_buffer_available(self) && !nt_buffer_grow(self, length))
    return false;
memcpy(self->ptr, what, length);
self->ptr += length;
  return true;
}


bool nt_buffer_appendb(nt_buffer_t *self, byte_t what) {
if (self->ptr >= self->end && !nt_buffer_grow(self, 1))
return false;
*(self->ptr++) = what;
return true;
}


bool nt_buffer_appendf(nt_buffer_t *self, const char *fmt, ...) {
va_list args;
size_t length, size;
int n;
  
length = 1;
size = nt_buffer_available(self);

do {
if (length > size) {
if (nt_buffer_grow(self, length))
return false;
size = nt_buffer_available(self);
}
    
va_start(args, fmt);
n = vsnprintf((char *)self->ptr, size, fmt, args);
va_end(args);
    
if (n >= 0) {
if (n < size) {
self->ptr += n;
return true;
}
length = n + 1;
}
else {
length = size << 1;
}
}
while (length > size);
  
return false;
}


ssize_t nt_buffer_indexof(nt_buffer_t *self, const byte_t *what, size_t n) {
  ssize_t i;
  size_t len = nt_buffer_length(self);
  // what happens if len > SSIZETMAX?
  for (i = 0; i < len; i++)
    if (memcmp((const char *)(self->start + i), (const char *)what, n) == 0)
      return i;
  return -1;
}


void nt_buffer_del(nt_buffer_t *self, size_t index, size_t count, size_t size) {
  size_t length, i, cpycount;
  void *srcp, *dstp;
  
  length = nt_buffer_length(self) / size;
  cpycount = count;
  assert(index < length);
  
  for (i = index; i < length; i += cpycount) {
    if (i + cpycount > length)
      cpycount = length - i;
    srcp = ADDROF(self, i + cpycount, size);
    dstp = ADDROF(self, i, size);
    AN(memcpy((void *)dstp, (const void *)srcp, size * cpycount));
  }
  
  self->ptr = (byte_t *)ADDROF(self, length - count, size);
  memset((void *)dstp, 0, size * count);
}
Something went wrong with that request. Please try again.