Use automatic allocation for assertions

This commit is contained in:
Rangi42
2024-02-22 14:26:12 -05:00
committed by Sylvie
parent 6e03504802
commit e022adf4a0
3 changed files with 19 additions and 35 deletions

View File

@@ -18,7 +18,7 @@ void out_RegisterNode(struct FileStackNode *node);
void out_ReplaceNode(struct FileStackNode *node);
void out_SetFileName(char *s);
void out_CreatePatch(uint32_t type, struct Expression const *expr, uint32_t ofs, uint32_t pcShift);
bool out_CreateAssert(enum AssertionType type, struct Expression const *expr,
void out_CreateAssert(enum AssertionType type, struct Expression const *expr,
char const *message, uint32_t ofs);
void out_WriteObject(void);

View File

@@ -10,6 +10,7 @@
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string>
#include <string.h>
#include <vector>
@@ -40,7 +41,7 @@ struct Patch {
struct Assertion {
struct Patch *patch;
struct Section *section;
char *message;
std::string message;
};
const char *objectName;
@@ -50,7 +51,7 @@ std::deque<struct Section *> sectionList;
// List of symbols to put in the object file
static std::vector<struct Symbol *> objectSymbols;
static std::deque<struct Assertion *> assertions;
static std::deque<struct Assertion> assertions;
static std::deque<struct FileStackNode *> fileStackNodes;
@@ -352,37 +353,26 @@ void out_CreatePatch(uint32_t type, struct Expression const *expr, uint32_t ofs,
}
// Creates an assert that will be written to the object file
bool out_CreateAssert(enum AssertionType type, struct Expression const *expr,
void out_CreateAssert(enum AssertionType type, struct Expression const *expr,
char const *message, uint32_t ofs)
{
struct Assertion *assertion = (struct Assertion *)malloc(sizeof(*assertion));
if (!assertion)
return false;
assertion->patch = allocpatch(type, expr, ofs);
assertion->message = strdup(message);
if (!assertion->message) {
free(assertion);
return false;
assertions.push_front({
.patch = allocpatch(type, expr, ofs),
.section = NULL,
.message = message,
});
}
assertions.push_front(assertion);
return true;
}
static void writeassert(struct Assertion *assert, FILE *f)
static void writeassert(struct Assertion &assert, FILE *f)
{
writepatch(assert->patch, f);
putstring(assert->message, f);
writepatch(assert.patch, f);
putstring(assert.message.c_str(), f);
}
static void freeassert(struct Assertion *assert)
static void freeassert(struct Assertion &assert)
{
free(assert->patch->rpn);
free(assert->patch);
free(assert);
free(assert.patch->rpn);
free(assert.patch);
}
static void writeFileStackNode(struct FileStackNode const *node, FILE *f)
@@ -456,7 +446,7 @@ void out_WriteObject(void)
putlong(assertions.size(), f);
for (struct Assertion *assert : assertions) {
for (struct Assertion &assert : assertions) {
writeassert(assert, f);
freeassert(assert);
}

View File

@@ -1089,10 +1089,7 @@ assert_type : %empty { $$ = ASSERT_ERROR; }
assert : T_POP_ASSERT assert_type relocexpr {
if (!rpn_isKnown(&$3)) {
if (!out_CreateAssert($2, &$3, "",
sect_GetOutputOffset()))
error("Assertion creation failed: %s\n",
strerror(errno));
out_CreateAssert($2, &$3, "", sect_GetOutputOffset());
} else if ($3.val == 0) {
failAssert($2);
}
@@ -1100,10 +1097,7 @@ assert : T_POP_ASSERT assert_type relocexpr {
}
| T_POP_ASSERT assert_type relocexpr T_COMMA string {
if (!rpn_isKnown(&$3)) {
if (!out_CreateAssert($2, &$3, $5,
sect_GetOutputOffset()))
error("Assertion creation failed: %s\n",
strerror(errno));
out_CreateAssert($2, &$3, $5, sect_GetOutputOffset());
} else if ($3.val == 0) {
failAssertMsg($2, $5);
}