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

Minor refactoring in bittree model

parent 53a3dd0e
No related branches found
No related tags found
No related merge requests found
...@@ -212,35 +212,43 @@ static int initialized(void * ptr, size_t size) { ...@@ -212,35 +212,43 @@ static 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 */
static size_t block_length(void* ptr) { static size_t block_length(void* ptr) {
bt_block * tmp = bt_find(ptr); bt_block * blk = 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(blk != NULL, "\\block_length of unallocated memory", NULL);
return tmp->size; return blk->size;
} }
static int allocated(void* ptr, size_t size, void *ptr_base) { static bt_block* allocated(void* ptr, size_t size, void *ptr_base) {
bt_block * blk = bt_find(ptr); bt_block * blk = bt_find(ptr);
if (blk == NULL)
return NULL;
#ifndef E_ACSL_WEAK_VALIDITY
bt_block * blk_base = bt_find(ptr_base); bt_block * blk_base = bt_find(ptr_base);
if (blk == NULL || blk_base == NULL || blk->ptr != blk_base->ptr) if (blk_base == NULL || blk->ptr != blk_base->ptr)
return false; return NULL;
return (blk->size - ((size_t)ptr - blk->ptr) >= size); #endif
return (blk->size - ((size_t)ptr - blk->ptr) >= size) ? blk : NULL;
} }
/* return whether the size bytes of ptr are readable/writable */ /** \brief Return 1 if a given memory location is read-only and 0 otherwise */
static int valid(void* ptr, size_t size, void *ptr_base, void *addr_of_base) { static int readonly (void *ptr) {
/* Many similarities with allocated (so far at least), but it is better
* to use this tandalone definition, otherwise the block needs to be looked
* up twice */
bt_block * blk = bt_find(ptr); bt_block * blk = bt_find(ptr);
bt_block * blk_base = bt_find(ptr_base); vassert(blk != NULL, "Readonly on unallocated memory", NULL);
if (blk == NULL || blk_base == NULL || blk->ptr != blk_base->ptr) return blk->is_readonly;
return false; }
return (blk->size - ((size_t)ptr - blk->ptr) >= size && !blk->is_readonly);
/* return whether the size bytes of ptr are readable/writable */
static int valid(void* ptr, size_t size, void *ptr_base, void *addrof_base) {
bt_block * blk = allocated(ptr, size, ptr_base);
if (blk != NULL)
return !blk->is_readonly;
return 0;
} }
/* return whether the size bytes of ptr are readable */ /* return whether the size bytes of ptr are readable */
static int valid_read(void* ptr, size_t size, void *ptr_base, void *addr_of_base) { static int valid_read(void* ptr, size_t size, void *ptr_base, void *addrof_base) {
return allocated(ptr, size, ptr_base); bt_block * blk = allocated(ptr, size, ptr_base);
return blk != NULL;
} }
/* return the base address of the block containing ptr */ /* return the base address of the block containing ptr */
...@@ -407,8 +415,7 @@ static int bittree_posix_memalign(void **memptr, size_t alignment, size_t size) ...@@ -407,8 +415,7 @@ static int bittree_posix_memalign(void **memptr, size_t alignment, size_t size)
return -1; return -1;
/* Make sure that the first argument to posix memalign is indeed allocated */ /* Make sure that the first argument to posix memalign is indeed allocated */
vassert(allocated((void*)memptr, sizeof(void*), (void*)memptr), DVALIDATE_RW_ACCESS((void*)memptr, sizeof(void*));
"\\invalid memptr in posix_memalign", NULL);
int res = native_posix_memalign(memptr, alignment, size); int res = native_posix_memalign(memptr, alignment, size);
if (!res) { if (!res) {
......
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