Skip to content
Snippets Groups Projects
Commit 5b2df86e authored by Kostyantyn Vorobyov's avatar Kostyantyn Vorobyov
Browse files

Renamed the main structure representing a memory block in a bittree

parent 5c157079
No related branches found
No related tags found
No related merge requests found
...@@ -102,7 +102,7 @@ static struct bittree { ...@@ -102,7 +102,7 @@ static struct bittree {
_Bool is_leaf; _Bool is_leaf;
size_t addr, mask; size_t addr, mask;
struct bittree * left, * right, * father; struct bittree * left, * right, * father;
struct _block * leaf; bt_block * leaf;
} * bt_root = NULL; } * bt_root = NULL;
/*unsigned cpt_mask = 0;*/ /*unsigned cpt_mask = 0;*/
...@@ -153,7 +153,7 @@ static size_t mask(size_t a, size_t b) { ...@@ -153,7 +153,7 @@ static size_t mask(size_t a, size_t b) {
@ ensures \valid(\result); @ ensures \valid(\result);
@ ensures \result->leaf == ptr; @ ensures \result->leaf == ptr;
@*/ @*/
static struct bittree * __get_leaf_from_block (struct _block * ptr) { static struct bittree * __get_leaf_from_block (bt_block * ptr) {
struct bittree * curr = bt_root; struct bittree * curr = bt_root;
assert(bt_root != NULL); assert(bt_root != NULL);
assert(ptr != NULL); assert(ptr != NULL);
...@@ -185,7 +185,7 @@ static struct bittree * __get_leaf_from_block (struct _block * ptr) { ...@@ -185,7 +185,7 @@ static struct bittree * __get_leaf_from_block (struct _block * ptr) {
/* the block we are looking for has to be in the tree */ /* the block we are looking for has to be in the tree */
/*@ requires \valid(ptr); /*@ requires \valid(ptr);
@*/ @*/
static void bt_remove (struct _block * ptr) { static void bt_remove (bt_block * ptr) {
struct bittree * leaf_to_delete = __get_leaf_from_block (ptr); struct bittree * leaf_to_delete = __get_leaf_from_block (ptr);
assert(leaf_to_delete->leaf == ptr); assert(leaf_to_delete->leaf == ptr);
...@@ -229,7 +229,7 @@ static void bt_remove (struct _block * ptr) { ...@@ -229,7 +229,7 @@ static void bt_remove (struct _block * ptr) {
@ assigns \nothing; @ assigns \nothing;
@ ensures \valid(\result); @ ensures \valid(\result);
@*/ @*/
static struct bittree * __most_similar_node (struct _block * ptr) { static struct bittree * __most_similar_node (bt_block * ptr) {
struct bittree * curr = bt_root; struct bittree * curr = bt_root;
size_t left_prefix, right_prefix; size_t left_prefix, right_prefix;
assert(ptr != NULL); assert(ptr != NULL);
...@@ -253,7 +253,7 @@ static struct bittree * __most_similar_node (struct _block * ptr) { ...@@ -253,7 +253,7 @@ static struct bittree * __most_similar_node (struct _block * ptr) {
/* add a block in the structure */ /* add a block in the structure */
/*@ requires \valid(ptr); /*@ requires \valid(ptr);
@*/ @*/
static void bt_insert (struct _block * ptr) { static void bt_insert (bt_block * ptr) {
struct bittree * new_leaf; struct bittree * new_leaf;
assert(ptr != NULL); assert(ptr != NULL);
...@@ -321,7 +321,7 @@ static void bt_insert (struct _block * ptr) { ...@@ -321,7 +321,7 @@ static void bt_insert (struct _block * ptr) {
@ ensures \valid(\result); @ ensures \valid(\result);
@ ensures \result == \null || \result->ptr == (size_t)ptr; @ ensures \result == \null || \result->ptr == (size_t)ptr;
@*/ @*/
static struct _block * bt_lookup (void * ptr) { static bt_block * bt_lookup (void * ptr) {
struct bittree * tmp = bt_root; struct bittree * tmp = bt_root;
assert(bt_root != NULL); assert(bt_root != NULL);
assert(ptr != NULL); assert(ptr != NULL);
...@@ -350,7 +350,7 @@ static struct _block * bt_lookup (void * ptr) { ...@@ -350,7 +350,7 @@ static struct _block * bt_lookup (void * ptr) {
/* return the block B containing ptr, such as : /* return the block B containing ptr, such as :
begin addr of B <= ptr < (begin addr + size) of B begin addr of B <= ptr < (begin addr + size) of B
or NULL if such a block does not exist */ or NULL if such a block does not exist */
static struct _block * bt_find (void * ptr) { static bt_block * bt_find (void * ptr) {
struct bittree * tmp = bt_root; struct bittree * tmp = bt_root;
if(bt_root == NULL || ptr == NULL) return NULL; if(bt_root == NULL || ptr == NULL) return NULL;
...@@ -396,7 +396,7 @@ static struct _block * bt_find (void * ptr) { ...@@ -396,7 +396,7 @@ static struct _block * bt_find (void * ptr) {
/* CLEAN */ /* CLEAN */
/*******************/ /*******************/
/* erase information about initialization of a block */ /* erase information about initialization of a block */
static void bt_clean_block_init (struct _block * ptr) { static void bt_clean_block_init (bt_block * ptr) {
if(ptr->init_ptr != NULL) { if(ptr->init_ptr != NULL) {
native_free(ptr->init_ptr); native_free(ptr->init_ptr);
ptr->init_ptr = NULL; ptr->init_ptr = NULL;
...@@ -405,7 +405,7 @@ static void bt_clean_block_init (struct _block * ptr) { ...@@ -405,7 +405,7 @@ static void bt_clean_block_init (struct _block * ptr) {
} }
/* erase all information about a block */ /* erase all information about a block */
static void bt_clean_block (struct _block * ptr) { static void bt_clean_block (bt_block * ptr) {
if(ptr) { if(ptr) {
bt_clean_block_init(ptr); bt_clean_block_init(ptr);
native_free(ptr); native_free(ptr);
...@@ -440,7 +440,7 @@ static void bt_clean () { ...@@ -440,7 +440,7 @@ static void bt_clean () {
#ifdef E_ACSL_DEBUG #ifdef E_ACSL_DEBUG
static void bt_print_block(struct _block * ptr) { static void bt_print_block(bt_block * ptr) {
if (ptr != NULL) { if (ptr != NULL) {
DLOG("%a; %lu Bytes; %slitteral; [init] : %d ", DLOG("%a; %lu Bytes; %slitteral; [init] : %d ",
(char*)ptr->ptr, ptr->size, (char*)ptr->ptr, ptr->size,
......
...@@ -32,7 +32,7 @@ ...@@ -32,7 +32,7 @@
#include "stdbool.h" #include "stdbool.h"
/*! \brief Structure representing an allocated memory block */ /*! \brief Structure representing an allocated memory block */
struct _block { struct bt_block {
size_t ptr; //!< Base address size_t ptr; //!< Base address
size_t size; //!< Block length (in bytes) size_t size; //!< Block length (in bytes)
unsigned char * init_ptr; //!< Per-bit initialization unsigned char * init_ptr; //!< Per-bit initialization
...@@ -41,30 +41,32 @@ struct _block { ...@@ -41,30 +41,32 @@ struct _block {
_Bool freeable; //!< True if a block can be de-allocated using `free` _Bool freeable; //!< True if a block can be de-allocated using `free`
}; };
typedef struct bt_block bt_block;
/*! \brief Remove a block from the structure */ /*! \brief Remove a block from the structure */
static void bt_remove(struct _block *b); static void bt_remove(bt_block *b);
/*! \brief Add a block to the structure */ /*! \brief Add a block to the structure */
static void bt_insert(struct _block *b); static void bt_insert(bt_block *b);
/*! \brief Return block B such that: `\base_addr(B->ptr) == ptr`. /*! \brief Return block B such that: `\base_addr(B->ptr) == ptr`.
NB: The function assumes that such a block exists. */ NB: The function assumes that such a block exists. */
static struct _block * bt_lookup(void *ptr); static bt_block * bt_lookup(void *ptr);
/*! \brief Return block B such that: /*! \brief Return block B such that:
`\base_addr(B->ptr) <= ptr < (\base_addr(B->ptr) + size)` `\base_addr(B->ptr) <= ptr < (\base_addr(B->ptr) + size)`
or NULL if such a block does not exist. */ or NULL if such a block does not exist. */
static struct _block * bt_find(void *ptr); static bt_block * bt_find(void *ptr);
/*! \brief Erase the contents of the structure */ /*! \brief Erase the contents of the structure */
static void bt_clean(void); static void bt_clean(void);
/*! \brief Print information about a given block */ /*! \brief Print information about a given block */
static void bt_print_block(struct _block *b); static void bt_print_block(bt_block *b);
/*! \brief Erase information about a block's initialization */ /*! \brief Erase information about a block's initialization */
static void bt_clean_block_init(struct _block *b); static void bt_clean_block_init(bt_block *b);
/*! \brief Erase all information about a given block */ /*! \brief Erase all information about a given block */
static void bt_clean_block(struct _block *b); static void bt_clean_block(bt_block *b);
#endif #endif
...@@ -64,11 +64,11 @@ size_t __get_memory_size(void) { ...@@ -64,11 +64,11 @@ size_t __get_memory_size(void) {
/**************************/ /**************************/
/* store the block of size bytes starting at ptr, the new block is returned. /* store the block of size bytes starting at ptr, the new block is returned.
* Warning: the return type is implicitly (struct _block*). */ * Warning: the return type is implicitly (bt_block*). */
void* __store_block(void* ptr, size_t size) { void* __store_block(void* ptr, size_t size) {
struct _block * tmp; bt_block * tmp;
DASSERT(ptr != NULL); DASSERT(ptr != NULL);
tmp = native_malloc(sizeof(struct _block)); tmp = native_malloc(sizeof(bt_block));
DASSERT(tmp != NULL); DASSERT(tmp != NULL);
tmp->ptr = (size_t)ptr; tmp->ptr = (size_t)ptr;
tmp->size = size; tmp->size = size;
...@@ -83,7 +83,7 @@ void* __store_block(void* ptr, size_t size) { ...@@ -83,7 +83,7 @@ void* __store_block(void* ptr, size_t size) {
/* remove the block starting at ptr */ /* remove the block starting at ptr */
void __delete_block(void* ptr) { void __delete_block(void* ptr) {
DASSERT(ptr != NULL); DASSERT(ptr != NULL);
struct _block * tmp = bt_lookup(ptr); bt_block * tmp = bt_lookup(ptr);
DASSERT(tmp != NULL); DASSERT(tmp != NULL);
bt_clean_block_init(tmp); bt_clean_block_init(tmp);
bt_remove(tmp); bt_remove(tmp);
...@@ -94,7 +94,7 @@ void __delete_block(void* ptr) { ...@@ -94,7 +94,7 @@ void __delete_block(void* ptr) {
* for further information, see malloc */ * for further information, see malloc */
void* __malloc(size_t size) { void* __malloc(size_t size) {
void * tmp; void * tmp;
struct _block * new_block; bt_block * new_block;
if(size <= 0) if(size <= 0)
return NULL; return NULL;
tmp = native_malloc(size); tmp = native_malloc(size);
...@@ -110,7 +110,7 @@ void* __malloc(size_t size) { ...@@ -110,7 +110,7 @@ void* __malloc(size_t size) {
/* free the block starting at ptr, /* free the block starting at ptr,
* for further information, see free */ * for further information, see free */
void __free(void* ptr) { void __free(void* ptr) {
struct _block * tmp; bt_block * tmp;
if(ptr == NULL) if(ptr == NULL)
return; return;
tmp = bt_lookup(ptr); tmp = bt_lookup(ptr);
...@@ -123,7 +123,7 @@ void __free(void* ptr) { ...@@ -123,7 +123,7 @@ void __free(void* ptr) {
} }
int __freeable(void* ptr) { int __freeable(void* ptr) {
struct _block * tmp; bt_block * tmp;
if(ptr == NULL) if(ptr == NULL)
return false; return false;
tmp = bt_lookup(ptr); tmp = bt_lookup(ptr);
...@@ -135,7 +135,7 @@ int __freeable(void* ptr) { ...@@ -135,7 +135,7 @@ int __freeable(void* ptr) {
/* resize the block starting at ptr to fit its new size, /* resize the block starting at ptr to fit its new size,
* for further information, see realloc */ * for further information, see realloc */
void* __realloc(void* ptr, size_t size) { void* __realloc(void* ptr, size_t size) {
struct _block * tmp; bt_block * tmp;
void * new_ptr; void * new_ptr;
/* ptr is NULL - malloc */ /* ptr is NULL - malloc */
if(ptr == NULL) if(ptr == NULL)
...@@ -205,7 +205,7 @@ void* __realloc(void* ptr, size_t size) { ...@@ -205,7 +205,7 @@ void* __realloc(void* ptr, size_t size) {
void* __calloc(size_t nbr_block, size_t size_block) { void* __calloc(size_t nbr_block, size_t size_block) {
void * tmp; void * tmp;
size_t size = nbr_block * size_block; size_t size = nbr_block * size_block;
struct _block * new_block; bt_block * new_block;
if(size <= 0) if(size <= 0)
return NULL; return NULL;
tmp = native_calloc(nbr_block, size_block); tmp = native_calloc(nbr_block, size_block);
...@@ -226,7 +226,7 @@ void* __calloc(size_t nbr_block, size_t size_block) { ...@@ -226,7 +226,7 @@ void* __calloc(size_t nbr_block, size_t size_block) {
/* mark the size bytes of ptr as initialized */ /* mark the size bytes of ptr as initialized */
void __initialize (void * ptr, size_t size) { void __initialize (void * ptr, size_t size) {
struct _block * tmp; bt_block * tmp;
if(!ptr) if(!ptr)
return; return;
...@@ -276,7 +276,7 @@ void __initialize (void * ptr, size_t size) { ...@@ -276,7 +276,7 @@ void __initialize (void * ptr, size_t size) {
/* mark all bytes of ptr as initialized */ /* mark all bytes of ptr as initialized */
void __full_init (void * ptr) { void __full_init (void * ptr) {
struct _block * tmp; bt_block * tmp;
if (ptr == NULL) if (ptr == NULL)
return; return;
...@@ -294,7 +294,7 @@ void __full_init (void * ptr) { ...@@ -294,7 +294,7 @@ void __full_init (void * ptr) {
/* mark a block as read-only */ /* mark a block as read-only */
void __readonly (void * ptr) { void __readonly (void * ptr) {
struct _block * tmp; bt_block * tmp;
if (ptr == NULL) if (ptr == NULL)
return; return;
tmp = bt_lookup(ptr); tmp = bt_lookup(ptr);
...@@ -310,7 +310,7 @@ void __readonly (void * ptr) { ...@@ -310,7 +310,7 @@ void __readonly (void * ptr) {
/* return whether the size bytes of ptr are initialized */ /* return whether the size bytes of ptr are initialized */
int __initialized (void * ptr, size_t size) { int __initialized (void * ptr, size_t size) {
unsigned i; unsigned i;
struct _block * tmp = bt_find(ptr); bt_block * tmp = bt_find(ptr);
if(tmp == NULL) if(tmp == NULL)
return false; return false;
...@@ -334,7 +334,7 @@ int __initialized (void * ptr, size_t size) { ...@@ -334,7 +334,7 @@ int __initialized (void * ptr, size_t size) {
/* return the length (in bytes) of the block containing ptr */ /* return the length (in bytes) of the block containing ptr */
size_t __block_length(void* ptr) { size_t __block_length(void* ptr) {
struct _block * tmp = bt_find(ptr); bt_block * tmp = bt_find(ptr);
/* Hard failure when un-allocated memory is used */ /* Hard failure when un-allocated memory is used */
vassert(tmp != NULL, "\\block_length of unallocated memory", NULL); vassert(tmp != NULL, "\\block_length of unallocated memory", NULL);
return tmp->size; return tmp->size;
...@@ -342,7 +342,7 @@ size_t __block_length(void* ptr) { ...@@ -342,7 +342,7 @@ size_t __block_length(void* ptr) {
/* return whether the size bytes of ptr are readable/writable */ /* return whether the size bytes of ptr are readable/writable */
int __valid(void* ptr, size_t size) { int __valid(void* ptr, size_t size) {
struct _block * tmp; bt_block * tmp;
if(ptr == NULL) if(ptr == NULL)
return false; return false;
tmp = bt_find(ptr); tmp = bt_find(ptr);
...@@ -353,7 +353,7 @@ int __valid(void* ptr, size_t size) { ...@@ -353,7 +353,7 @@ int __valid(void* ptr, size_t size) {
/* return whether the size bytes of ptr are readable */ /* return whether the size bytes of ptr are readable */
int __valid_read(void* ptr, size_t size) { int __valid_read(void* ptr, size_t size) {
struct _block * tmp; bt_block * tmp;
if(ptr == NULL) if(ptr == NULL)
return false; return false;
tmp = bt_find(ptr); tmp = bt_find(ptr);
...@@ -363,14 +363,14 @@ int __valid_read(void* ptr, size_t size) { ...@@ -363,14 +363,14 @@ int __valid_read(void* ptr, size_t size) {
/* return the base address of the block containing ptr */ /* return the base address of the block containing ptr */
void* __base_addr(void* ptr) { void* __base_addr(void* ptr) {
struct _block * tmp = bt_find(ptr); bt_block * tmp = bt_find(ptr);
vassert(tmp != NULL, "\\base_addr of unallocated memory", NULL); vassert(tmp != NULL, "\\base_addr of unallocated memory", NULL);
return (void*)tmp->ptr; return (void*)tmp->ptr;
} }
/* return the offset of `ptr` within its block */ /* return the offset of `ptr` within its block */
int __offset(void* ptr) { int __offset(void* ptr) {
struct _block * tmp = bt_find(ptr); bt_block * tmp = bt_find(ptr);
vassert(tmp != NULL, "\\offset of unallocated memory", NULL); vassert(tmp != NULL, "\\offset of unallocated memory", NULL);
return ((size_t)ptr - tmp->ptr); return ((size_t)ptr - tmp->ptr);
} }
...@@ -408,7 +408,7 @@ void __e_acsl_memory_init(int *argc_ref, char ***argv_ref, size_t ptr_size) { ...@@ -408,7 +408,7 @@ void __e_acsl_memory_init(int *argc_ref, char ***argv_ref, size_t ptr_size) {
/**********************/ /**********************/
#ifdef E_ACSL_DEBUG #ifdef E_ACSL_DEBUG
/*! \brief print the information about a block */ /*! \brief print the information about a block */
void __e_acsl_print_block (struct _block * ptr) { void __e_acsl_print_block (bt_block * ptr) {
bt_print_block(ptr); bt_print_block(ptr);
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment