Exemplo n.º 1
0
int32_t h_heap_push(h_heap_st *hp, void *data)
{
    uint32_t curr_index;

    if (hp->nbase >= hp->__size_base) {
        if (hp->__fixed)
            return -1;

        hp->__size_base =
            hp->__size_base ?  hp->__size_base * 2 : DEFAULT_HEAP_SIZE;
        hp->bases = h_realloc(hp->bases, hp->__size_base * sizeof(void *));
        if (!hp->bases)
        {
            return -1;
        }
    }

    curr_index = hp->nbase++;
    hp->bases[curr_index] = data;

    while (curr_index != 0) {
        uint32_t parent_index = (curr_index - 1) >> 1;

        if (hp->__cmp(hp->bases[curr_index], hp->bases[parent_index]) >= 0)
            break;

        swap_node(hp->bases[parent_index], hp->bases[curr_index]);
        curr_index = parent_index;
    }
    return 0;
}
Exemplo n.º 2
0
static inline int darray_resize(darray_t *array, size_t newsize)
{
    array->max = newsize;
    array->contents = h_realloc(array->contents, array->max * sizeof(void *));
    check_mem(array->contents);
    return 0;
error:
    return -1;
}
Exemplo n.º 3
0
static int
_co_node_set_str(_treenode_t *n, const char *value, const size_t vlen)
{
  CHECK(n != NULL, "Invalid node supplied.");
  CHECK(n->value != NULL, "Invalid node supplied.");
  switch(CO_TYPE(n->value))
  {
    case _str8:
      CHECK(vlen <= UINT8_MAX, "Value too large for type str8.");
      if(vlen != (((co_str8_t *)(n->value))->_len))
      {
        n->value = h_realloc(n->value, (size_t)(vlen + sizeof(co_str8_t) - 1));
      }
      CHECK_MEM(memmove((((co_str8_t *)(n->value))->data), value, vlen));
      (((co_str8_t *)(n->value))->_len) = (uint8_t)vlen;
      break;
    case _str16:
      CHECK(vlen <= UINT16_MAX, "Value too large for type str16.");
      if(vlen != (((co_str16_t *)(n->value))->_len))
      {
        n->value = h_realloc(n->value, (size_t)(vlen + sizeof(co_str16_t) - 1));
      }
      CHECK_MEM(memmove((((co_str16_t *)(n->value))->data), value, vlen));
      (((co_str16_t *)(n->value))->_len) = (uint16_t)vlen;
      break;
    case _str32:
      CHECK(vlen <= UINT32_MAX, "Value too large for type str32.");
      if(vlen != (((co_str32_t *)(n->value))->_len))
      {
        n->value = h_realloc(n->value, (size_t)(vlen + sizeof(co_str32_t) - 1));
      }
      CHECK_MEM(memmove((((co_str32_t *)(n->value))->data), value, vlen));
      (((co_str32_t *)(n->value))->_len) = (uint32_t)vlen;
      break;
    default:
      SENTINEL("Specified object is not a string.");
      break;
  }

  return 1;
error:
  return 0;
}
Exemplo n.º 4
0
void IOBuf_resize(IOBuf *buf, size_t new_size)
{
    buf->buf = h_realloc(buf->buf, new_size);
    buf->len = new_size;
}