Skip to content

Commit

Permalink
Added the autoptr_zero_obj() procedure for zeroing all bytes of the
Browse files Browse the repository at this point in the history
managed object excluding the autoptr struct.

The autoptr_unbind() precedure now calls autoptr_dtor() which requires
a valid autoptr struct during cleanup. This call occurs after the call
to the object destructor.

The autoptr_zero_obj() procedure is to be used in place of memset()
for zeroing the managed object within object destructors in order to
preserve the base autoptr struct.
  • Loading branch information
jgraha8 committed Feb 15, 2019
1 parent 1c03478 commit e8465a2
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
20 changes: 16 additions & 4 deletions include/autoptr.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
#include <assert.h>
#include <pthread.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>

#ifdef __cplusplus
extern "C" {
Expand All @@ -53,7 +55,6 @@ extern "C" {
AUTOPTR_LOCK(ptr); \
assert(ptr); \
const unsigned int __magic = ((struct autoptr *)(ptr))->__magic; \
assert(__magic == AUTOPTR_MAGIC); \
if (__magic != AUTOPTR_MAGIC) { \
fprintf(stderr, "autoptr_assert_magic: magic = 0x%8x != 0x%8x\n", __magic, \
AUTOPTR_MAGIC); \
Expand All @@ -80,13 +81,13 @@ extern "C" {
*/
struct autoptr {
unsigned int __magic; ///< Magic number
int r_count; ///< Reference count
pthread_mutex_t mutex;
bool allocd; ///< Allocation flag; indicates if an object is heap-allocated
int r_count; ///< Reference count
size_t obj_len; ///< Length in bytes of managed object
void (*obj_dtor)(void *); ///< Destructor for the managed object
struct autoptr *manager; ///< Manager object of a managed contiguous set (e.g. 1st in vector of objects)
size_t num_managed; ///< Number of objects of a managed contiguous set
bool allocd; ///< Allocation flag; indicates if an object is heap-allocated
};

/**
Expand All @@ -107,10 +108,21 @@ void autoptr_ctor(void *ptr, size_t obj_len, void (*obj_dtor)(void *));
*/
void autoptr_dtor(void *ptr);

/**
* @brief Zeros all bytes of the object excluding the autoptr struct
*
* This is used in place of @c memset() for zeroing all bytes of the
* object in order to preserve the state of the (base) autoptr struct
* that is managed by libautoptr.
*
* @param ptr Address of memory managed object
*/
void autoptr_zero_obj(void *ptr);

/**
* @brief Sets the object length and destructor function.
*
* Typically used to reasign the object length and destructor for a
* Typically used to reassign the object length and destructor for a
* derived data-structure.
*
* @param ptr Address of memory managed object
Expand Down
14 changes: 14 additions & 0 deletions src/autoptr.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@

void autoptr_ctor(void *ptr, size_t obj_len, void (*obj_dtor)(void *))
{
assert( obj_len >= sizeof(struct autoptr));

memset(ptr, 0, sizeof(struct autoptr));

AUTOPTR(ptr)->__magic = AUTOPTR_MAGIC;
Expand All @@ -45,8 +47,19 @@ void autoptr_dtor(void *ptr)
{
autoptr_assert(ptr);
pthread_mutex_destroy(&AUTOPTR(ptr)->mutex);
memset(ptr, 0, sizeof(struct autoptr));
}

void autoptr_zero_obj(void *ptr)
{
autoptr_assert(ptr);
assert( AUTOPTR(ptr)->obj_len >= sizeof(struct autoptr));

const size_t zero_len = AUTOPTR(ptr)->obj_len - sizeof(struct autoptr);
memset((char *)ptr + sizeof(struct autoptr), 0, zero_len);


}
void autoptr_set_obj(void *ptr, size_t obj_len, void (*obj_dtor)(void *))
{
AUTOPTR_M(ptr)->obj_len = obj_len;
Expand Down Expand Up @@ -85,6 +98,7 @@ void autoptr_unbind(void **ptr)
manager->obj_dtor(obj);
}

autoptr_dtor(manager);
if (allocd) {
// We free the manager object
free(manager);
Expand Down

0 comments on commit e8465a2

Please sign in to comment.