Example #1
0
int main(int argc, char* argv[]) {
    int err, i;
    char key[10], value[10];    
    CHUNK* chunk;
    
    /* create database file */
    err = chunk_create(FILENAME, HTABLE_SIZE);
    if (err) {
        printf("Error: cant create chunk file\n");
        return -1;
    }

    /* open database file */
    chunk = malloc(sizeof(CHUNK));    
    err = chunk_open(chunk, FILENAME, CACHE_SIZE);
    if (err) {
        free(chunk);
        printf("Error: problem with open chunk file\n");
        return -2;
    }

    /* insert  */
    for(i=0; i<SIZE; i++) {
        sprintf(key, "%s%d", "key", i);
        sprintf(value, "%s%d", "value", i);
        chunk_set(chunk, key, value);
    }

    /* remove */
    printf("chunk_remove key5 %d\n", chunk_remove(chunk, "key5"));
    printf("chunk_remove key7 %d\n", chunk_remove(chunk, "key7"));
    
    /* get */
    for(i=0; i<SIZE; i++) {
        sprintf(key, "%s%d", "key", i);
        printf("chunk_get %s = %s\n",key, chunk_get(chunk, key));
    }    
    
    /* forEach */
    chunk_for_each(chunk, display);

    /* close */
    chunk_close(chunk);    
    free(chunk);
    
    return  0;
}
Example #2
0
static void
_chunk_fusion(struct malloc_infos *list, struct chunk *prev, struct chunk *next)
{
  if ((char *)next == (char *)prev + prev->size)
    {
      chunk_remove_free(list, prev);
      chunk_remove(list, next);
      prev->size += next->size + sizeof (struct chunk);
      chunk_append_free(list, prev);
    }
}
Example #3
0
void
chunk_give_back(struct malloc_infos *list)
{
  struct chunk *chunk;

  chunk = list->lmain;
  if (!chunk)
    return;
  if (chunk->size < MALLOC_GIVE_BACK_SIZE)
    return;
  if (chunk->type != MALLOC_CHUNK_FREE)
    return;
#if _MALLOC_USES_SBRK
  if ((char *) chunk + (chunk->size + sizeof (struct chunk)) > __curent_brk)
    return;
  chunk_remove(list, chunk);
  brk(chunk);
#endif
}