ext/iodine/fio.c
/*
 * allocates memory using `mmap`, but enforces block size alignment.
 * requires page aligned `len`.
 *
 * `align_shift` is used to move the memory page alignment to allow for a single
 * page allocation header. align_shift MUST be either 0 (normal) or 1 (single
 * page header). Other values might cause errors.
 */
allocates memory using `mmap`, but enforces block size alignment.
requires page aligned `len`.

`align_shift` is used to move the memory page alignment to allow for a single
page allocation header. align_shift MUST be either 0 (normal) or 1 (single
page header). Other values might cause errors.
allocates memory using ` mmap ` but enforces block size alignment . requires page aligned ` len ` . ` align_shift ` is used to move the memory page alignment to allow for a single page allocation header . align_shift MUST be either 0 ( normal ) or 1 ( single page header ) . Other values might cause errors .
static inline void *sys_alloc(size_t len, uint8_t is_indi) {
  void *result;
  static void *next_alloc = NULL;
#ifdef MAP_ALIGNED
  result =
      mmap(next_alloc, len, PROT_READ | PROT_WRITE,
           MAP_PRIVATE | MAP_ANONYMOUS | MAP_ALIGNED(FIO_MEMORY_BLOCK_SIZE_LOG),
           -1, 0);
#else
  result = mmap(next_alloc, len, PROT_READ | PROT_WRITE,
                MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
#endif
  if (result == MAP_FAILED)
    return NULL;
  if (((uintptr_t)result & FIO_MEMORY_BLOCK_MASK)) {
    munmap(result, len);
    result = mmap(NULL, len + FIO_MEMORY_BLOCK_SIZE, PROT_READ | PROT_WRITE,
                  MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
    if (result == MAP_FAILED) {
      return NULL;
    }
    const uintptr_t offset =
        (FIO_MEMORY_BLOCK_SIZE - ((uintptr_t)result & FIO_MEMORY_BLOCK_MASK));
    if (offset) {
      munmap(result, offset);
      result = (void *)((uintptr_t)result + offset);
    }
    munmap((void *)((uintptr_t)result + len), FIO_MEMORY_BLOCK_SIZE - offset);
  }
  if (is_indi ==
      0) 
    next_alloc =
        (void *)((uintptr_t)result +
                 (FIO_MEMORY_BLOCK_SIZE * (FIO_MEMORY_BLOCKS_PER_ALLOCATION)));
  else 
    next_alloc = (void *)((uintptr_t)result + (is_indi * ((uintptr_t)1 << 30)));
  return result;
}
static inline void * sys_alloc ( size_t len , uint8_t is_indi ) { void * result ; static void * next_alloc = NULL ; #ifdef MAP_ALIGNED result = mmap ( next_alloc , len , PROT_READ | PROT_WRITE , MAP_PRIVATE | MAP_ANONYMOUS | MAP_ALIGNED ( FIO_MEMORY_BLOCK_SIZE_LOG ) , -1 , 0 ) ; #else result = mmap ( next_alloc , len , PROT_READ | PROT_WRITE , MAP_PRIVATE | MAP_ANONYMOUS , -1 , 0 ) ; #endif if ( result == MAP_FAILED ) return NULL ; if ( ( ( uintptr_t ) result & FIO_MEMORY_BLOCK_MASK ) ) { munmap ( result , len ) ; result = mmap ( NULL , len + FIO_MEMORY_BLOCK_SIZE , PROT_READ | PROT_WRITE , MAP_PRIVATE | MAP_ANONYMOUS , -1 , 0 ) ; if ( result == MAP_FAILED ) { return NULL ; } const uintptr_t offset = ( FIO_MEMORY_BLOCK_SIZE - ( ( uintptr_t ) result & FIO_MEMORY_BLOCK_MASK ) ) ; if ( offset ) { munmap ( result , offset ) ; result = ( void * ) ( ( uintptr_t ) result + offset ) ; } munmap ( ( void * ) ( ( uintptr_t ) result + len ) , FIO_MEMORY_BLOCK_SIZE - offset ) ; } if ( is_indi == 0 ) next_alloc = ( void * ) ( ( uintptr_t ) result + ( FIO_MEMORY_BLOCK_SIZE * ( FIO_MEMORY_BLOCKS_PER_ALLOCATION ) ) ) ; else next_alloc = ( void * ) ( ( uintptr_t ) result + ( is_indi * ( ( uintptr_t ) 1 << 30 ) ) ) ; return result ; }
allocates memory using `mmap`, but enforces block size alignment.
allocates memory using ` mmap ` but enforces block size alignment .
/* hope for the best? */ /* advance by a block's allocation size for next allocation */ /* add 1TB for realloc */