MentOS  0.8.0
The Mentoring Operating System
Classes | Macros | Functions | Variables
stdlib.c File Reference

Classes

struct  malloc_header_t
 A structure that holds the information about an allocated chunk of memory through malloc. More...
 

Macros

#define MALLOC_MAGIC_NUMBER   0x600DC0DE
 Number which identifies a memory area allocated through a call to malloc(), calloc() or realloc().
 

Functions

static void * malloc_header_to_ptr (malloc_header_t *header)
 Extract the actual pointer to the allocated memory from the malloc header. More...
 
static malloc_header_tptr_to_malloc_header (void *ptr)
 Extract the malloc header, from the actual pointer to the allocated memory. More...
 
void * malloc (unsigned int size)
 Provides dynamically allocated memory. More...
 
void * calloc (size_t num, size_t size)
 Allocates a block of memory for an array of num elements. More...
 
void * realloc (void *ptr, size_t size)
 Reallocates the given area of memory. More...
 
void free (void *ptr)
 Frees dynamically allocated memory. More...
 
void srand (unsigned x)
 Allows to set the seed of the random value generator. More...
 
unsigned rand (void)
 Generates a random unsigned integer between 0 and RAND_MAX. More...
 
float randf (void)
 Generates a random floating point number between 0 and 1. More...
 
int randint (int lb, int ub)
 Generates a random integer between lb and ub. More...
 
unsigned randuint (unsigned lb, unsigned ub)
 Generates a random unsigned between lb and ub. More...
 
float randfloat (float lb, float ub)
 Generates a random float between lb and ub. More...
 

Variables

static unsigned rseed = 0
 Seed used to generate random numbers.
 

Detailed Description

Function Documentation

◆ calloc()

void* calloc ( size_t  num,
size_t  size 
)

Allocates a block of memory for an array of num elements.

Parameters
numThe number of elements.
sizeThe size of an element.
Returns
A pointer to the allocated memory.

◆ free()

void free ( void *  ptr)

Frees dynamically allocated memory.

Parameters
ptrThe pointer to the allocated memory.

◆ malloc()

void* malloc ( unsigned int  size)

Provides dynamically allocated memory.

Parameters
sizeThe amount of memory to allocate.
Returns
A pointer to the allocated memory.

◆ malloc_header_to_ptr()

static void* malloc_header_to_ptr ( malloc_header_t header)
inlinestatic

Extract the actual pointer to the allocated memory from the malloc header.

Parameters
headerthe header we are using.
Returns
a pointer to the allocated memory.

◆ ptr_to_malloc_header()

static malloc_header_t* ptr_to_malloc_header ( void *  ptr)
inlinestatic

Extract the malloc header, from the actual pointer to the allocated memory.

Parameters
ptrthe pointer we use.
Returns
the malloc header.

◆ rand()

unsigned rand ( void  )

Generates a random unsigned integer between 0 and RAND_MAX.

Returns
the random value.

◆ randf()

float randf ( void  )

Generates a random floating point number between 0 and 1.

Returns
the random value.

◆ randfloat()

float randfloat ( float  lb,
float  ub 
)

Generates a random float between lb and ub.

Parameters
lbthe lower-bound value.
ubthe upper-bound value.
Returns
the random value.

◆ randint()

int randint ( int  lb,
int  ub 
)

Generates a random integer between lb and ub.

Parameters
lbthe lower-bound value.
ubthe upper-bound value.
Returns
the random value.

◆ randuint()

unsigned randuint ( unsigned  lb,
unsigned  ub 
)

Generates a random unsigned between lb and ub.

Parameters
lbthe lower-bound value.
ubthe upper-bound value.
Returns
the random value.

◆ realloc()

void* realloc ( void *  ptr,
size_t  size 
)

Reallocates the given area of memory.

Parameters
ptrThe pointer to the memory to reallocate.
sizeThe new size for the memory.
Returns
A pointer to the new portion of memory.

It must be previously allocated by malloc(), calloc() or realloc() and not yet freed with a call to free or realloc. Otherwise, the results are undefined.

◆ srand()

void srand ( unsigned  x)

Allows to set the seed of the random value generator.

Parameters
xThe new seed.