Skip to content

Commit

Permalink
Enforce consistent style and dead code elimination
Browse files Browse the repository at this point in the history
  • Loading branch information
jserv committed Jan 24, 2020
1 parent 8172192 commit 48ba811
Show file tree
Hide file tree
Showing 8 changed files with 197 additions and 116 deletions.
135 changes: 82 additions & 53 deletions console.c

Large diffs are not rendered by default.

8 changes: 2 additions & 6 deletions console.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
typedef bool (*cmd_function)(int argc, char *argv[]);

/* Information about each command */

/* Organized as linked list in alphabetical order */
typedef struct CELE cmd_ele, *cmd_ptr;
struct CELE {
Expand Down Expand Up @@ -42,9 +43,6 @@ void add_param(char *name,
char *doccumentation,
setter_function setter);

/* Execute a sequence of commands read from a file */
bool interpret_file(FILE *fp);

/* Extract integer from text and store at loc */
bool get_int(char *vname, int *loc);

Expand All @@ -54,10 +52,8 @@ void add_quit_helper(cmd_function qf);
/* Turn echoing on/off */
void set_echo(bool on);

/* Is it time to quit the command loop? */
bool cmd_done();

/* Complete command interpretation */

/* Return true if no errors occurred */
bool finish_cmd();

Expand Down
25 changes: 20 additions & 5 deletions harness.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,17 @@

/* Value at start of every allocated block */
#define MAGICHEADER 0xdeadbeef

/* Value when deallocate block */
#define MAGICFREE 0xffffffff

/* Value at end of every block */
#define MAGICFOOTER 0xbeefdead

/* Byte to fill newly malloced space with */
#define FILLCHAR 0x55

/** Data structures used by our code **/
/* Data structures used by our code */

/*
* Represent allocated blocks as doubly-linked list, with
Expand All @@ -40,8 +43,10 @@ typedef struct BELE {

static block_ele_t *allocated = NULL;
static size_t allocated_count = 0;

/* Percent probability of malloc failure */
int fail_probability = 0;

static bool cautious_mode = true;
static bool noallocate_mode = false;
static bool error_occurred = false;
Expand Down Expand Up @@ -77,6 +82,7 @@ static block_ele_t *find_header(void *p)
report_event(MSG_ERROR, "Attempting to free null block");
error_occurred = true;
}

block_ele_t *b = (block_ele_t *) ((size_t) p - sizeof(block_ele_t));
if (cautious_mode) {
/* Make sure this is really an allocated block */
Expand All @@ -93,13 +99,15 @@ static block_ele_t *find_header(void *p)
error_occurred = true;
}
}

if (b->magic_header != MAGICHEADER) {
report_event(
MSG_ERROR,
"Attempted to free unallocated or corrupted block. Address = %p",
p);
error_occurred = true;
}

return b;
}

Expand All @@ -112,24 +120,27 @@ static size_t *find_footer(block_ele_t *b)
}

/*
Implementation of application functions
* Implementation of application functions
*/
void *test_malloc(size_t size)
{
if (noallocate_mode) {
report_event(MSG_FATAL, "Calls to malloc disallowed");
return NULL;
}

if (fail_allocation()) {
report_event(MSG_WARN, "Malloc returning NULL");
return NULL;
}

block_ele_t *new_block =
malloc(size + sizeof(block_ele_t) + sizeof(size_t));
if (!new_block) {
report_event(MSG_FATAL, "Couldn't allocate any more memory");
error_occurred = true;
}

// cppcheck-suppress nullPointerRedundantCheck
new_block->magic_header = MAGICHEADER;
// cppcheck-suppress nullPointerRedundantCheck
Expand All @@ -141,10 +152,12 @@ void *test_malloc(size_t size)
new_block->next = allocated;
// cppcheck-suppress nullPointerRedundantCheck
new_block->prev = NULL;

if (allocated)
allocated->prev = new_block;
allocated = new_block;
allocated_count++;

return p;
}

Expand All @@ -154,6 +167,7 @@ void test_free(void *p)
report_event(MSG_FATAL, "Calls to free disallowed");
return;
}

if (!p)
return;

Expand Down Expand Up @@ -189,9 +203,9 @@ char *test_strdup(const char *s)
{
size_t len = strlen(s) + 1;
void *new = test_malloc(len);

if (!new)
return NULL;

return (char *) memcpy(new, s, len);
}

Expand Down Expand Up @@ -245,9 +259,9 @@ bool exception_setup(bool limit_time)
alarm(0);
time_limited = false;
}
if (error_message) {

if (error_message)
report_event(MSG_ERROR, error_message);
}
error_message = "";
return false;
}
Expand All @@ -270,6 +284,7 @@ void exception_cancel()
alarm(0);
time_limited = false;
}

jmp_ready = false;
error_message = "";
}
Expand Down
5 changes: 4 additions & 1 deletion harness.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ void test_free(void *p);
char *test_strdup(const char *s);

#ifdef INTERNAL

/* Report number of allocated blocks */
size_t allocation_check();

/* Probability of malloc failing, expressed as percent */
int fail_probability;
extern int fail_probability;

/*
* Set/unset cautious mode.
Expand Down Expand Up @@ -56,10 +57,12 @@ void exception_cancel();
void trigger_exception(char *msg);

#else /* !INTERNAL */

/* Tested program use our versions of malloc and free */
#define malloc test_malloc
#define free test_free
#define strdup test_strdup

#endif

#endif /* LAB0_HARNESS_H */
Loading

0 comments on commit 48ba811

Please sign in to comment.