示例#1
0
  static bool
space_alloc_mmap(struct Mempool * const mempool, const size_t cap)
{
  const size_t hcap = ((cap + MEMPOOL_UNIT - 1) / MEMPOOL_UNIT) * MEMPOOL_UNIT;
  assert(hcap >= cap);
  void * const m = huge_alloc(hcap);
  // MAP_UNINITIALIZED need kernel configuration: CONFIG_MMAP_ALLOW_UNINITIALIZED. it's unsafe
  if (m) {
    mempool->using_mmap = true;
    mempool->space = m;
    mempool->max = (uint64_t)hcap;
    return true;
  } else {
    mempool->using_mmap = false;
    mempool->space = (typeof(mempool->space))malloc(cap);
    mempool->max = (uint64_t)cap;
    return (mempool->space)?true:false;
  }
}
示例#2
0
static void compaction_initial(struct Compaction *const comp,
                               struct DB *const db,
                               struct VirtualContainer *const vc,
                               uint64_t nr_feed) {
    bzero(comp, sizeof(*comp));
    comp->start_bit = vc->start_bit;
    comp->sub_bit = vc->start_bit + 3;
    comp->gen_bc = (comp->sub_bit >= BC_START_BIT);
    assert(nr_feed <= vc->cc.count);
    assert(vc->cc.count <= DB_CONTAINER_NR);
    comp->nr_feed = nr_feed;
    comp->db = db;
    comp->vc = vc;
    comp->cm_to = db->cms[comp->sub_bit / 3];

    // alloc arenas
    uint8_t *const arena = huge_alloc(TABLE_ALIGN);
    assert(arena);
    comp->arena = arena;
    // old mts & mtids
    for (uint64_t i = 0; i < nr_feed; i++) {
        struct MetaTable *const mt = vc->cc.metatables[i];
        assert(mt);
        comp->mts_old[i] = mt;
        comp->mtids_old[i] = mt->mtid;
    }

    // new tables
    for (uint64_t i = 0; i < 8u; i++) {
        struct Table *const table = table_alloc_default(1.8);
        assert(table);
        comp->tables[i] = table;
    }

    // mbcs_old (if exists else NULL)
    for (uint64_t i = 0; i < 8u; i++) {
        if (!vc->sub_vc[i]) {
            vc->sub_vc[i] = vc_create(comp->sub_bit);
        }
        comp->mbcs_old[i] = vc->sub_vc[i]->cc.bc;
    }
}
示例#3
0
  EXPORT_FUNC
  TT_Error  TT_Alloc( ULong  Size, void**  P )
  {
    Int  i;


    if ( !P )
      return TT_Err_Invalid_Argument;
        /* Also see below for another case of "invalid argument". */

    if ( Size > 0 )
    {
      if ( Size > ( UINT_MAX & ~0xFu ) )
        *P = (void*)huge_alloc( Size );
      else
        *P = (void*)malloc( Size );
      if ( !*P )
        return TT_Err_Out_Of_Memory;

#ifndef TT_CONFIG_OPTION_THREAD_SAFE
      TTMemory_Allocated    += Size;
      TTMemory_MaxAllocated += Size;
#endif

#ifdef DEBUG_MEMORY

      num_alloc++;

      i = 0;
      while ( i < MAX_TRACKED_BLOCKS && pointers[i].base != NULL )
        i++;

      if ( i >= MAX_TRACKED_BLOCKS )
        fail_alloc++;
      else
      {
        pointers[i].base = *P;
        pointers[i].size = Size;
      }

#else

      if ( Size > ( UINT_MAX & ~0xFu ) )
      {
        i = 0;
        while ( i < MAX_TRACKED_BIGCHUNKS && pointers[i].base != NULL )
          i++;

        if ( i >= MAX_TRACKED_BIGCHUNKS )
          /* We fail badly here. Increase MAX_TRACKED_BIGCHUNKS if needed. */
          return TT_Err_Invalid_Argument;
        else
        {
          pointers[i].base = *P;
          pointers[i].size = Size;
        }
      }

#endif /* DEBUG_MEMORY */

      if ( Size > ( UINT_MAX & ~0xFu ) )
      {
        char TT_HUGE_PTR * p = (char TT_HUGE_PTR *) *P;
        ULong        left = (ULong)Size;
        size_t       toClear;

        while ( left )
        {
          toClear = (left > 0xFF00) ? 0xFF00 : left;
          MEM_Set( p, 0, toClear );
          left -= (ULong) toClear;
          p    += toClear;
        }
      }
      else
        MEM_Set( *P, 0, Size );
    }
    else
      *P = NULL;

    return TT_Err_Ok;
  }