示例#1
0
/* Resize the string */
int str_resize(str_t *str, size_t reqlen)
{
    size_t nsize;
    char *nstr;

    if (reqlen <= str->size)
        return STR_TRUE;

    /* Double the size each time, amortized cost is 2 */
    nsize = 1;
    while (nsize < reqlen)
        nsize <<= 1;

    nstr = (char*)X_MALLOC(nsize);
    if (!nstr)
        return STR_FALSE;

    if (str->c_str)
    {
        memcpy(nstr, str->c_str, str->size);
		if (str->is_owner)
			X_FREE(str->c_str);
    }

    str->c_str = nstr;
	str->is_owner = 1;
    str->size = nsize;

    return STR_TRUE;
}
示例#2
0
/* Find a node / Insert a new node */
rbtn_t *rbtn_findins(rbtn_t **root, int key, void* data, int force,
    int (*compar)(void*, void*), int sortbykey, int nofixup, int* added)
{
    rbtn_t *current, *parent, *x;
    int c;

    current = (*root == 0) ? RBTN_NIL : *root;
    parent = NULL;
    if (added)
        *added = 0;

    while (current != RBTN_NIL)
    {
        c = (sortbykey) ? (key - current->key) : compar(data, current->data);
        if (c == 0)
            return current;

        parent = current;
        current = (c < 0) ? (current->left) : (current->right);
    }

    if (!force)
        return NULL;

    x = (rbtn_t*)X_MALLOC(sizeof(rbtn_t));
    x->parent = parent;
    x->left = RBTN_NIL;
    x->right = RBTN_NIL;
    x->color = RBTN_RED;
    x->key = key;
    x->data = data;

    if (parent)
    {
        c = (sortbykey) ? (key - parent->key) : compar(data, parent->data);
        if (c < 0)
            parent->left = x;
        else
            parent->right = x;
    }
    else
        *root = x;

    if (!nofixup)
        rbtn_insfix(root, x);
    if (added)
        *added = 1;

    return x;
}
示例#3
0
/* Compact the used memory for this string */
void str_compact(str_t *str)
{
    char *nstr;
    if ((str->len + 1 >= str->size) || !str->c_str)
        return;

    /* Shrink */
    nstr = (char*)X_MALLOC(str->len+1);
    if (!nstr)
        return;

    memcpy(nstr, str->c_str, str->len+1);
    X_FREE(str->c_str);
    str->c_str = nstr;
    str->size = str->len+1;
}
示例#4
0
文件: asestrbuf.cpp 项目: ahiguti/ase
ase_string_buffer::ase_string_buffer(size_type initial_alloc_size)
  : bufalloc(0), buf_end(0), bufalloc_end(0)
{
  /* we use malloc/free instead of new/delete in order to make debugging
    easier */
  const size_t bufalloc_len = (initial_alloc_size > alloc_size_min)
    ? initial_alloc_size : alloc_size_min;
  bufalloc = static_cast<value_type *>(
    X_MALLOC(bufalloc_len * sizeof(value_type)));
  if (!bufalloc) {
    throw std::bad_alloc();
  }
  buf_end = bufalloc;
  bufalloc_end = bufalloc + bufalloc_len;
  assert_valid();
}
示例#5
0
文件: asestrbuf.cpp 项目: ahiguti/ase
void
ase_string_buffer::reset(size_type initial_alloc_size)
{
  const size_t len = (initial_alloc_size > alloc_size_min)
    ? initial_alloc_size : alloc_size_min;
  if (len != alloc_size()) {
    value_type *p = static_cast<value_type *>(
      X_MALLOC(len * sizeof(value_type)));
    if (!p) {
      throw std::bad_alloc();
    }
    X_FREE(bufalloc);
    bufalloc = p;
    bufalloc_end = bufalloc + len;
  }
  buf_end = bufalloc;
  assert_valid();
}
示例#6
0
static void * blowfish_ctx_alloc( void )
{
    return X_MALLOC(NULL, sizeof( blowfish_context ) );
}
示例#7
0
static void * des3_ctx_alloc( void )
{
    return X_MALLOC(NULL, sizeof( des3_context ) );
}
示例#8
0
static void * camellia_ctx_alloc( void )
{
    return X_MALLOC(NULL, sizeof( camellia_context ) );
}
示例#9
0
static void * aes_ctx_alloc( void )
{
    return X_MALLOC(NULL, sizeof( aes_context ) );
}