Skip to content

Commit

Permalink
Implement an untested memory allocation function and pray for forgive…
Browse files Browse the repository at this point in the history
…ness.
  • Loading branch information
cr1901 committed Feb 16, 2019
1 parent 0ccc5b2 commit 23b5a59
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
2 changes: 1 addition & 1 deletion cisparse.h
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ typedef struct
memory used by each tuple's pointers won't go out of scope. */
/* Return true for success, false for "unable to complete task". */
bool (* foreach)(cis_tuple_t curr, void * user);
bool (* alloc)(void ** mem_ptr, size_t size, void * user);
bool (* alloc)(void ** mem_ptr, cis_alloc_req_t type, size_t size, void * user);
bool debug; /* Print parser debugging info if true. */
void * user;

Expand Down
37 changes: 35 additions & 2 deletions dumpcis.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ typedef struct
void cis_perror(dumpcis_error_t err);
print_error_t cis_print();
bool foreach_tuple(cis_tuple_t curr, void * user);
bool alloc_tuple(void ** mem_ptr, size_t size, void * user);
bool alloc_tuple(void ** mem_ptr, cis_alloc_req_t type, size_t size, void * user);

int main()
{
Expand Down Expand Up @@ -242,7 +242,40 @@ bool foreach_tuple(cis_tuple_t curr, void * user)
return false;
}

bool alloc_tuple(void ** mem_ptr, size_t size, void * user)
bool alloc_tuple(void ** mem_ptr, cis_alloc_req_t type, size_t size, void * user)
{
dumpcis_user_t * ptrs = user;
bool alloc_rc = false;

/* I hope I'm forgiven one day for this... */
#define BOUNDED_ALLOC_AND_RETURN(_array, _offset, _type) do { \
if((_offset + size) < (sizeof(_array)/sizeof(_type))) \
{ \
*mem_ptr = &_array[_offset]; \
_offset += size; \
return true; \
} \
else \
{ \
return false; \
} \
} while(false)


switch(type)
{
case ALLOC_CHAR_STR:
BOUNDED_ALLOC_AND_RETURN(string, ptrs->string, char);
case ALLOC_CHAR_PTR:
BOUNDED_ALLOC_AND_RETURN(str_ptr, ptrs->str_ptr, char *);
case ALLOC_MFC_ADDR:
BOUNDED_ALLOC_AND_RETURN(mfc_addr, ptrs->mfc_addr, cis_mfc_addr_t);
case ALLOC_DEV_INFO:
BOUNDED_ALLOC_AND_RETURN(device_info, ptrs->device_info, cis_device_info_t);
default:
return false;
}

/* Unreachable. */
return false;
}

0 comments on commit 23b5a59

Please sign in to comment.