void * __cdecl iso_aligned_offset_malloc(size_t size, size_t alignment, size_t offset)
{
    size_t alignment_mask;
    size_t alloc_size;
    uintptr_t pvAlloc, pvData, uintptr_offset;
    ALIGN_BLOCK_HEADER *pBlockHdr;
#ifdef _DEBUG
    uintptr_t nFrontPaddedSize;
    uintptr_t nLastPaddedSize;
#endif

    _ASSERT(offset == 0 || offset < size);
    if (offset >= size)
        return NULL;

    /* 如果不是2的幂次方, 则调整到最接近的2的幂次方 */
    _ASSERT(IS_POWER_OF_2(alignment));
#if 1
    if (NOT_IS_POWER_OF_2(alignment)) {
        alignment = iso_next_power_of_2(alignment);
        _ASSERT(IS_POWER_OF_2(alignment));
    }
#endif

    alignment = (alignment > sizeof(uintptr_t)) ? alignment : sizeof(uintptr_t);
    _ASSERT(alignment > 0);

    alignment_mask = alignment - 1;
    uintptr_offset = (0 - offset) & (sizeof(uintptr_t) - 1);

    // alloc_size align to alignment bytes (isn't must need)
    alloc_size = size + alignment_mask + sizeof(ALIGN_BLOCK_HEADER) + uintptr_offset;

    pvAlloc = (uintptr_t)::malloc(alloc_size);
    if (pvAlloc != (uintptr_t)NULL) {
        // data pointer align to alignment bytes
        pvData = (uintptr_t)((((uintptr_t)pvAlloc + alignment_mask + sizeof(ALIGN_BLOCK_HEADER)
            + uintptr_offset + offset) & (~alignment_mask)) - offset);

        pBlockHdr = (ALIGN_BLOCK_HEADER *)(pvData - uintptr_offset) - 1;
        _ASSERT((uintptr_t)pBlockHdr >= pvAlloc);

#if _USE_ALIGN_SIGN_
        ::memset((void *)pBlockHdr->Sign, _cAlignSignFill, ALIGN_SIGN_SIZE);
#endif
        pBlockHdr->pvAlloc = (void *)pvAlloc;

#ifdef _DEBUG
        // for debug
        nFrontPaddedSize = (uintptr_t)pvData - (uintptr_t)pvAlloc;
        nLastPaddedSize  = (uintptr_t)alloc_size - (uintptr_t)size - nFrontPaddedSize;

        _ASSERT(nFrontPaddedSize >= sizeof(ALIGN_BLOCK_HEADER));
        _ASSERT(nLastPaddedSize >= 0);
#endif
        return (void *)pvData;
    }

    return (void *)NULL;
}
Exemple #2
0
static int imdr_create_empty(struct imdr *imdr, size_t root_size,
				size_t entry_align)
{
	struct imd_root_pointer *rp;
	struct imd_root *r;
	struct imd_entry *e;
	ssize_t root_offset;

	if (!imdr->limit)
		return -1;

	/* root_size and entry_align should be a power of 2. */
	assert(IS_POWER_OF_2(root_size));
	assert(IS_POWER_OF_2(entry_align));

	if (!imdr->limit)
		return -1;

	/*
	 * root_size needs to be large enough to accomodate root pointer and
	 * root book keeping structure. The caller needs to ensure there's
	 * enough room for tracking individual allocations.
	 */
	if (root_size < (sizeof(*rp) + sizeof(*r)))
		return -1;

	/* For simplicity don't allow sizes or alignments to exceed LIMIT_ALIGN. */
	if (root_size > LIMIT_ALIGN || entry_align > LIMIT_ALIGN)
		return -1;

	/* Additionally, don't handle an entry alignment > root_size. */
	if (entry_align > root_size)
		return -1;

	rp = imdr_get_root_pointer(imdr);

	root_offset = -(ssize_t)root_size;
	/* Set root pointer. */
	imdr->r = relative_pointer((void *)imdr->limit, root_offset);
	r = imdr_root(imdr);
	imd_link_root(rp, r);

	memset(r, 0, sizeof(*r));
	r->entry_align = entry_align;

	/* Calculate size left for entries. */
	r->max_entries = root_num_entries(root_size);

	/* Fill in first entry covering the root region. */
	r->num_entries = 1;
	e = &r->entries[0];
	imd_entry_assign(e, CBMEM_ID_IMD_ROOT, 0, root_size);

	printk(BIOS_DEBUG, "IMD: root @ %p %u entries.\n", r, r->max_entries);

	return 0;
}
Exemple #3
0
/* fh_hash_create(keylen,entries)
 *
 * Allocates a table large enough to hold 'entries' entries of size 'keylen'.
 * Note that 'entries' must be a power of two.
 */
static
fh_hash_t *
fh_hash_create(size_t entries)
{
	fh_hash_t	*hash;

	if (!IS_POWER_OF_2(entries))
		gasneti_fatalerror("fh_hash_create requires a power of 2!");

	hash = (fh_hash_t *) gasneti_malloc(sizeof(fh_hash_t));
	if (hash == NULL)
		gasneti_fatalerror("Can't allocate memory for hash structure");
	memset(hash, 0, sizeof(fh_hash_t));

	hash->fh_table   = (void **) gasneti_calloc(entries, sizeof(void *));
	hash->fh_mask    = entries-1;
	hash->fh_entries = entries;
	#ifdef FH_HASH_STATS
		hash->fh_col_table = (int *) gasneti_malloc(entries * sizeof(int));
		/*printf("hash create: entries=%d, mask=%x\n", entries, entries-1);*/
		hash->fh_used = 0;
		hash->fh_collisions = 0;
	#endif
	
	return hash;
}
Exemple #4
0
int char_vector_expand(char_vector_t* vec, size_t desired_size) {
	char *new_vec;
	size_t new_size;

	if(vec == NULL || desired_size == 0)
		return 0;

	if(vec->size >= desired_size)
		return 1;

	// Resize to the next power of 2 from the desired size.
	if(!IS_POWER_OF_2(desired_size)) {
		new_size = 1;
		while (new_size < desired_size) { new_size *= 2; };
	} else {
		new_size = desired_size;
	}

	new_vec = (char*)realloc(vec->vector, new_size);
	if(new_vec == NULL) {
		// Try to allocate exactly the desired size, maybe we don't have enough
		// memory for the next power of 2.
		new_size = desired_size;

		new_vec = (char*)realloc(vec->vector, new_size);
		if(new_vec == NULL)
			return 0;
	}

	vec->vector = new_vec;
	vec->size = new_size;
	return 1;
}
Exemple #5
0
/*
 * Get spinlock to use based on the signal name.
 */
spinlock_t *
listener_get_lock(const char *name)
{
	STATIC_ASSERT(IS_POWER_OF_2(LISTENER_HASH_MASK + 1));

	return &listener_access[string_mix_hash(name) & LISTENER_HASH_MASK];
}
Exemple #6
0
static int __buddy_dump(struct buddy_pool* self)
{
    fprintf(stderr,"basic info :\n");
    fprintf(stderr,"============================================================\n");
    fprintf(stderr,"min_order = %2d\torder = %2d\t\tpool_size = %llu\nbh=%p\tbuffer = %p\n",
            self->min_order,self->order,self->pool_size,self->bh,self->buffer);
    fprintf(stderr,"============================================================\n");
    fprintf(stderr,"alloc info :");

    fprintf(stderr,"\n=============================================================================");
    int i = 0;
    int level = -1;
    int bh_num = 2<<(self->order - self->min_order) - 1;
    for( i = 0;i < bh_num;i++)
    {
        if(i == 31)
        {
            fprintf(stderr,"\n......skipped.........");
            break;
        }
        if(IS_POWER_OF_2(i+1))
        {
            level++;
            fprintf(stderr,"\n level (%d): %2d",level,self->bh[i]);
        }
        else
        {
            fprintf(stderr," %2d",self->bh[i]);
        }
    }
    fprintf(stderr,"\n=============================================================================\n");
    return 0;
}
Exemple #7
0
void* MemBuffer::Allocate(size_t allocation_size, size_t alignment) {
  YASSERT(IS_POWER_OF_2(alignment),
          "Alignment must be a power of 2: %u",
          static_cast<uint32_t>(alignment));
  size_t buffer =
      reinterpret_cast<size_t>(Allocate(allocation_size + alignment));
  return reinterpret_cast<void*>(ROUND_UP(buffer, alignment));
}
Exemple #8
0
static int imdr_recover(struct imdr *imdr)
{
	struct imd_root_pointer *rp;
	struct imd_root *r;
	uintptr_t low_limit;
	size_t i;

	if (!imdr->limit)
		return -1;

	rp = imdr_get_root_pointer(imdr);

	if (!imd_root_pointer_valid(rp))
		return -1;

	r = relative_pointer(rp, rp->root_offset);

	/* Confirm the root and root pointer are just under the limit. */
	if (ALIGN_UP((uintptr_t)&r->entries[r->max_entries], LIMIT_ALIGN) !=
			imdr->limit)
		return -1;

	if (r->num_entries > r->max_entries)
		return -1;

	/* Entry alignment should be power of 2. */
	if (!IS_POWER_OF_2(r->entry_align))
		return -1;

	low_limit = (uintptr_t)relative_pointer(r, r->max_offset);

	/* If no max_offset then lowest limit is 0. */
	if (low_limit == (uintptr_t)r)
		low_limit = 0;

	for (i = 0; i < r->num_entries; i++) {
		uintptr_t start_addr;
		const struct imd_entry *e = &r->entries[i];

		if (e->magic != IMD_ENTRY_MAGIC)
			return -1;

		start_addr = (uintptr_t)relative_pointer(r, e->start_offset);
		if (start_addr  < low_limit)
			return -1;
		if (start_addr >= imdr->limit ||
				(start_addr + e->size) > imdr->limit)
			return -1;
	}

	/* Set root pointer. */
	imdr->r = r;

	return 0;
}
size_t __cdecl iso_adjust_alignment(size_t alignment)
{
    alignment = iso_next_power_of_2(alignment);
    _ASSERT(IS_POWER_OF_2(alignment));

#if 1
    alignment = (alignment > sizeof(uintptr_t)) ? alignment : sizeof(uintptr_t);
    _ASSERT(alignment > 0);
#endif

    return alignment;
}
Exemple #10
0
/*
 * Function Definition
 */
static inline int32_t fat_is_valid_sec_sz(uint8_t *sec_sz, uint32_t len)
{
  uint32_t val = 0;

  if (len != 2) {
    return 0;
  }

  val = (uint32_t)GET_UNALIGNED_LE16(sec_sz);

  return IS_POWER_OF_2(val) && (val >= SECTOR_SIZE) && (val <= SECTOR_SIZE_MAX);
}
Exemple #11
0
static uint32_t to_power_of_2(uint32_t n)
{
    if(!IS_POWER_OF_2(n)) {
        int bit;
        for(bit = 0; bit < 32; bit++) {
            if(n & (1 << bit))
                break;
        }

        n = 1 << (bit+1);
    }

    return n;
}
int Occurrence::calculateShiftValue(int divisor)
{
    assert(divisor > 0);
    assert(IS_POWER_OF_2(divisor));

    // m_sampleRate is a power of 2, count what bit is set
    unsigned int v = divisor;
    unsigned int c = 0; // c accumulates the total bits set in v

    while(v != 1)
    {
        v >>= 1;
        ++c;
    }
    assert(1 << c == divisor);
    return c;
}
Exemple #13
0
struct buddy_pool*
buddy_create(unsigned int order,unsigned int min_order)
{
    if(order >=64 || min_order >= order)
    {
        return (struct buddy_pool*) -1;
    }

    
    struct buddy_pool* self = malloc(sizeof(struct buddy_pool));
    if(self == NULL)
    {
        goto malloc_self_failed;
    }
    pthread_mutex_init(&self->buddy_lock,NULL);
    self->order = order;
    self->min_order = min_order;
    self->pool_size = 1<<(order);
    int bh_num = (1<<(order - self->min_order))*2 -1;
    int ret = posix_memalign((void **)&(self->bh),4096,bh_num);
    if(ret < 0 )
    {
        goto malloc_bh_failed;
    }

    ret = posix_memalign((void**)&(self->buffer),4096,self->pool_size);
    if(ret < 0 )
    {
        goto malloc_buffer_failed;
    }
    int i = 0;
    int current_order = order + 1;
    for(i = 0;i< bh_num ;i++)
    {
        if(IS_POWER_OF_2(i+1))
            current_order--;
        self->bh[i] = current_order;
    }
    return self ;
malloc_buffer_failed:
    free(self->bh);
malloc_bh_failed :
    free(self);
malloc_self_failed:
    return (struct buddy_pool*)(-1);
}
Exemple #14
0
/*******************************************************************************
 *
 * Implementation of the vector container.
 *
 ******************************************************************************/
EXPORT_SYM enum sl_error
sl_create_vector
  (size_t data_size,
   size_t data_alignment,
   struct mem_allocator* specific_allocator,
   struct sl_vector** out_vec)
{
  struct mem_allocator* allocator = NULL;
  struct sl_vector* vec = NULL;
  enum sl_error err = SL_NO_ERROR;

  if(!out_vec || !data_size) {
    err = SL_INVALID_ARGUMENT;
    goto error;
  }
  if(!IS_POWER_OF_2(data_alignment)) {
    err = SL_ALIGNMENT_ERROR;
    goto error;
  }
  allocator = specific_allocator ? specific_allocator : &mem_default_allocator;
  vec = MEM_CALLOC(allocator, 1, sizeof(struct sl_vector));
  if(vec == NULL) {
    err = SL_MEMORY_ERROR;
    goto error;
  }
  vec->allocator = allocator;
  vec->data_size = data_size;
  vec->data_alignment = data_alignment;

exit:
  if(out_vec)
    *out_vec = vec;
  return err;

error:
  if(vec) {
    ASSERT(allocator);
    MEM_FREE(allocator, vec);
    vec = NULL;
  }
  goto exit;
}
Exemple #15
0
/**
 * Free a block from file.
 */
static void
big_ffree(DBM *db, size_t bno)
{
	DBMBIG *dbg = db->big;
	long bmap;
	size_t i;

	STATIC_ASSERT(IS_POWER_OF_2(BIG_BITCOUNT));

	if (-1 == dbg->fd && -1 == big_open(dbg)) {
		g_warning("sdbm: \"%s\": cannot free block #%ld",
			sdbm_name(db), (long) bno);
		return;
	}

	/*
	 * Block number must be positive, and we cannot free a bitmap block.
	 * If we end-up doing it, then it means data in the .pag was corrupted,
	 * so we do not assert but fail gracefully.
	 */

	if (!size_is_positive(bno) || 0 == (bno & (BIG_BITCOUNT - 1))) {
		g_warning("sdbm: \"%s\": attempt to free invalid block #%ld",
			sdbm_name(db), (long) bno);
		return;
	}

	g_assert(size_is_positive(bno));	/* Can never free block 0 (bitmap!) */
	g_assert(bno & (BIG_BITCOUNT - 1));	/* Cannot be a bitmap block */

	bmap = bno / BIG_BITCOUNT;			/* Bitmap handling this block */
	i = bno & (BIG_BITCOUNT - 1);		/* Index within bitmap */

	/*
	 * Likewise, if the block falls in a bitmap we do not know about yet,
	 * the .pag was corrupted.
	 */

	if (bmap >= dbg->bitmaps) {
		g_warning("sdbm: \"%s\": "
			"freed block #%ld falls within invalid bitmap #%ld (max %ld)",
			sdbm_name(db), (long) bno, bmap, dbg->bitmaps - 1);
		return;
	}

	if (!fetch_bitbuf(db, bmap))
		return;

	/*
	 * Again, freeing a block that is already marked as being freed is
	 * a severe error but can happen if the bitmap cannot be flushed to disk
	 * at some point, hence it cannot be an assertion.
	 */

	if (!bit_field_get(dbg->bitbuf, i)) {
		g_warning("sdbm: \"%s\": freed block #%ld was already marked as free",
			sdbm_name(db), (long) bno);
		return;
	}

	bit_field_clear(dbg->bitbuf, i);
	dbg->bitbuf_dirty = TRUE;
}
Exemple #16
0
// 
// Handle command line arguments
//
void parseOverlapLongOptions(int argc, char** argv)
{
    bool die = false;
    for (char c; (c = getopt_long(argc, argv, shortopts, longopts, NULL)) != -1;) 
    {
        std::istringstream arg(optarg != NULL ? optarg : "");
        switch (c) 
        {
            case 'm': arg >> opt::minOverlap; break;
            case 'o': arg >> opt::outFile; break;
            case 'e': arg >> opt::errorRate; break;
            case 't': arg >> opt::numThreads; break;
            case 'l': arg >> opt::seedLength; break;
            case 's': arg >> opt::seedStride; break;
            case 'd': arg >> opt::sampleRate; break;
            case 'f': arg >> opt::targetFile; break;
            case OPT_EXACT: opt::bExactIrreducible = true; break;
            case 'x': opt::bIrreducibleOnly = false; break;
            case '?': die = true; break;
            case 'v': opt::verbose++; break;
            case OPT_HELP:
                std::cout << OVERLAP_LONG_USAGE_MESSAGE;
                exit(EXIT_SUCCESS);
            case OPT_VERSION:
                std::cout << OVERLAP_LONG_VERSION_MESSAGE;
                exit(EXIT_SUCCESS);
        }
    }

    if (argc - optind < 1) 
    {
        std::cerr << SUBPROGRAM ": missing arguments\n";
        die = true;
    } 
    else if (argc - optind > 1) 
    {
        std::cerr << SUBPROGRAM ": too many arguments\n";
        die = true;
    }

    if(opt::numThreads <= 0)
    {
        std::cerr << SUBPROGRAM ": invalid number of threads: " << opt::numThreads << "\n";
        die = true;
    }

    if(!IS_POWER_OF_2(opt::sampleRate))
    {
        std::cerr << SUBPROGRAM ": invalid parameter to -d/--sample-rate, must be power of 2. got: " << opt::sampleRate << "\n";
        die = true;
    }

    if (die) 
    {
        std::cout << "\n" << OVERLAP_LONG_USAGE_MESSAGE;
        exit(EXIT_FAILURE);
    }

    // Validate parameters
    if(opt::errorRate <= 0)
        opt::errorRate = 0.0f;
    
    if(opt::seedLength < 0)
        opt::seedLength = 0;

    if(opt::seedLength > 0 && opt::seedStride <= 0)
        opt::seedStride = opt::seedLength;
    
    // Parse the input filenames
    opt::readsFile = argv[optind++];

    if(opt::outFile.empty())
    {
        std::string prefix = stripFilename(opt::readsFile);
        if(!opt::targetFile.empty())
        {
            prefix.append(1,'.');
            prefix.append(stripFilename(opt::targetFile));
        }
        opt::outFile = prefix + ASQG_EXT + GZIP_EXT;
    }
}
Exemple #17
0
int32_t fat_fill_sb(struct fat_super_block *sb)
{
  int32_t offset = 0;
  size_t sz = 0;
  int32_t is_fat32_fs = 0;
  int32_t ret = 0;

  /*
   * Fill in FAT boot sector
   */
  offset = 0;
  ret = io_fseek(offset);
  if (ret != 0) {
    return -1;
  }

  sz = sizeof(struct fat_boot_sector);
  ret = io_fread((uint8_t *)&sb->bs, sz);
  if (ret != 0) {
    memset((void *)&sb->bs, 0, sz);
    return -1;
  }

  if (!fat_is_valid_sec_sz((uint8_t *)sb->bs.sector_size, 2)
      || !IS_POWER_OF_2(sb->bs.sec_per_clus)
      || sb->bs.reserved == 0
      || sb->bs.fats == 0
      || !fat_is_valid_media((uint32_t)sb->bs.media)) {
    return -1;
  }

  ret = fat_is_fat32_fs((const struct fat_super_block *)sb, &is_fat32_fs);
  if (ret != 0) {
    return -1;
  }

  /*
   * Fill in FAT boot bsx
   */
  if (is_fat32_fs) {
    offset = FAT32_BSX_OFFSET;
  } else {
    offset = FAT16_BSX_OFFSET;
  }

  ret = io_fseek(offset);
  if (ret != 0) {
    return -1;
  }

  sz = sizeof(struct fat_boot_bsx);
  ret = io_fread((uint8_t *)&sb->bb, sz);
  if (ret != 0) {
    memset((void *)&sb->bb, 0, sz);
    return -1;
  }

  if (is_fat32_fs) {
    /*
     * Fill in FAT32 boot fsinfo
     */
    offset = sb->bs.info_sector == 0 ? GET_UNALIGNED_LE16(sb->bs.sector_size) : sb->bs.info_sector * GET_UNALIGNED_LE16(sb->bs.sector_size);

    ret = io_fseek(offset);
    if (ret != 0) {
      return -1;
    }

    sz = sizeof(struct fat_boot_fsinfo);
    ret = io_fread((uint8_t *)&sb->bf, sz);
    if (ret != 0) {
      memset((void *)&sb->bf, 0, sz);
      return -1;
    }

    if (sb->bf.signature1 != FAT_FSINFO_SIG1 || sb->bf.signature2 != FAT_FSINFO_SIG2) {
      return -1;
    }
  }

  return 0;
}
void * __cdecl iso_aligned_offset_realloc(void *ptr, size_t new_size, size_t alignment, size_t offset)
{
    uintptr_t pvAlloc, pvData, uintptr_offset, mov_sz;
    ALIGN_BLOCK_HEADER *pBlockHdr, *s_pBlockHdr;

    if (ptr == NULL)
        return iso_aligned_offset_malloc(new_size, alignment, offset);

    if (new_size == 0) {
        iso_aligned_free(ptr);
        return NULL;
    }

    s_pBlockHdr = (ALIGN_BLOCK_HEADER *)((uintptr_t)ptr & ~(sizeof(uintptr_t) - 1)) -1;

#ifdef _DEBUG
    if (_iso_check_bytes((unsigned char *)ptr - NO_MANS_LAND_SIZE, s_cNoMansLandFill, NO_MANS_LAND_SIZE)) {
        // We don't know where (file, linenum) ptr was allocated
        _RPT1(_CRT_ERROR, "The block at 0x%p was not allocated by _aligned routines, use realloc()", ptr);
        errno = EINVAL;
        return NULL;
    }
#endif

#if _USE_ALIGN_SIGN_
    if (!_iso_check_bytes((unsigned char *)s_pBlockHdr->Sign, _cAlignSignFill, ALIGN_SIGN_SIZE)) {
        // We don't know where (file, linenum) ptr was allocated
        _RPT1(_CRT_ERROR, "Damage before 0x%p which was allocated by aligned routine\n", ptr);
    }
#endif

#ifdef __linux__
    mov_sz = malloc_usable_size(s_pBlockHdr->pvAlloc) - ((uintptr_t)ptr - (uintptr_t)s_pBlockHdr->pvAlloc);
#else
    mov_sz = _msize(s_pBlockHdr->pvAlloc) - ((uintptr_t)ptr - (uintptr_t)s_pBlockHdr->pvAlloc);
#endif

    /* validation section */
    _ASSERT(offset == 0 || offset < new_size);

    /* 如果不是2的幂次方, 则调整到最接近的2的幂次方 */
    _ASSERT(IS_POWER_OF_2(alignment));
    if (NOT_IS_POWER_OF_2(alignment)) {
        alignment = iso_next_power_of_2(alignment);
        _ASSERT(IS_POWER_OF_2(alignment));
    }

    alignment = (alignment > sizeof(uintptr_t) ? alignment : sizeof(uintptr_t)) -1;

    uintptr_offset = (0 - offset) & (sizeof(uintptr_t) - 1);

    if ((pvAlloc = (uintptr_t)::malloc(new_size + alignment + sizeof(ALIGN_BLOCK_HEADER) + uintptr_offset)) == (uintptr_t)NULL)
        return NULL;

    pvData =((pvAlloc + alignment + sizeof(ALIGN_BLOCK_HEADER) + uintptr_offset + offset) & ~alignment) - offset;
    pBlockHdr = (ALIGN_BLOCK_HEADER *)(pvData - uintptr_offset) - 1;
#if _USE_ALIGN_SIGN_
    ::memset((void *)pBlockHdr->Sign, _cAlignSignFill, ALIGN_SIGN_SIZE);
#endif
    pBlockHdr->pvAlloc = (void *)pvAlloc;

    ::memcpy((void *)pvData, ptr, mov_sz > new_size ? new_size : mov_sz);

    ::free(s_pBlockHdr->pvAlloc);

    return (void *)pvData;
}