(MOD) Fixed 1 bug in Memory_Reallocate: Now the size of inst is updating with the given size used for reallocation.

(MOD) Replaced struct member prev from UnknownStatus, NormalStatus and ErrorStatus from NULL to &UnknownStatus, &NormalStatus and &ErrorStatus.

(MOD) Rewrote Status_Equal, StatusUtils_Dump, StatusUtils_IsRecursive and StatusUtils_Depth in response to the change of struct member prev from UnknownStatus, NormalStatus and ErrorStatus.

(MOD) Defined 1 more macro "shift".
This commit is contained in:
2024-06-27 16:01:01 +08:00
parent 310586ab86
commit 9f2b44bf99
5 changed files with 76 additions and 51 deletions

View File

@@ -33,7 +33,10 @@ Status Memory_Reallocate(Memory *inst, size_t size)
/* When failed on reallocating. */
state(!(inst->addr = realloc(inst->addr, size)),
apply(error(InsufficientMemory, "Unsuccessful reallocation was received.")));
apply(error(InsufficientMemory, "Cannot successfully reallocate.")));
/* Update size from inst. */
inst->size = size;
return apply(NormalStatus);
}

View File

@@ -143,9 +143,9 @@ int StatusUtils_Depth(Status *inst);
// ---------------------ELEMENTARY-------------------------
DEFSTATUS(UnknownStatus, -1, "An unknown status.", STATUS_UNKNOWN, NULL);
DEFSTATUS(NormalStatus, 0, "A normal status.", STATUS_NORMAL, NULL);
DEFSTATUS(ErrorStatus, 1, "An error status.", STATUS_ERROR, NULL);
DEFSTATUS(UnknownStatus, -1, "An unknown status.", STATUS_UNKNOWN, &UnknownStatus);
DEFSTATUS(NormalStatus, 0, "A normal status.", STATUS_NORMAL, &NormalStatus);
DEFSTATUS(ErrorStatus, 1, "An error status.", STATUS_ERROR, &ErrorStatus);
// ----------------------EXTENDED--------------------------
@@ -328,7 +328,15 @@ static inline Status PrintStatus(Status s)
static inline void PrintStatusDump(Status s)
{
/* Create dump. */
/* Calculate depth for dumping. */
const int dump_len = StatusUtils_Depth(&s);
/* Skip when "s" is either unavailable or is at the buttom of status stack. */
if (dump_len == -1) {
PrintStatus(s);
return;
}
Status dump[dump_len];
Status current = s;
dump[0] = current; // Put self at leading.
@@ -336,6 +344,7 @@ static inline void PrintStatusDump(Status s)
// StatusUtils_Dump will only access (storage) the prev.
// It does not include this status itself.
StatusUtils_Dump(&current, &dump[i]);
current = *current.prev;
}
@@ -348,22 +357,6 @@ static inline void PrintStatusDump(Status s)
unsure(PrintStatus(dump[i]), !_.value, {
(void)fprintf(stderr, "Unable to literalise.\n");
})
// seek(PrintStatus(dump[i]), { // Get returning status.
// /* Handle TraditionalFunctionReturn. */
// nest(_, __, unsure(__, !__.value, { // No bytes were written to buffer.
// (void)fprintf(stderr, "Unable to literalise.\n");
// return;
// }));
// // Handle abnormal status.
// nest(_, __, notok(__, {
// /* Output the description as explanation. */
// (void)fprintf(stderr, "%s\n", __.description);
// return;
// }));
// });
}
}

View File

@@ -27,13 +27,12 @@ bool Status_Equal(Status *stat1, Status *stat2)
state(stat1 == NULL || stat2 == NULL, false);
return (
!strcmp(stat1->identity, stat2->identity) &&
stat1->value == stat2->value &&
!strcmp(stat1->description, stat2->description) &&
stat1->characteristic == stat2->characteristic &&
Location_Equal(&stat1->loc, &stat2->loc) &&
((StatusUtils_HasPrev(*stat1) && StatusUtils_HasPrev(*stat2))
? Status_Equal(stat1->prev, stat2->prev)
: true)
stat1->prev == stat2->prev
);
}
@@ -87,7 +86,8 @@ bool StatusUtils_IsOkay(Status stat)
bool StatusUtils_IsRecursive(Status stat)
{
return (stat.prev && stat.prev == &stat);
// return (stat.prev && stat.prev == stat.prev->prev);
return (stat.prev && Status_Equal(&stat, stat.prev));
}
void StatusUtils_Dump(Status *inst, Status *store)
@@ -100,26 +100,6 @@ void StatusUtils_Dump(Status *inst, Status *store)
*store = *inst->prev;
}
// void StatusUtils_Dump(Status *inst, Status **store, int idx)
// {
// /* Skip when having invalid inst, store or idx. */
// svoid(!inst || !store || idx < 0 || StatusUtils_IsRecursive(*inst));
// // store[idx] = *inst;
// *store[idx] = (Status){
// .identity = inst->identity,
// .value = inst->value,
// .description = inst->description,
// .characteristic = inst->characteristic,
// .loc = inst->loc,
// .prev = inst->prev
// };
// (void)printf("idx: %d\n", idx);
// StatusUtils_Dump(inst->prev, store, (idx - 1));
// }
int StatusUtils_Depth(Status *stat)
{
/* Skip unavailable stat. */
@@ -131,6 +111,9 @@ int StatusUtils_Depth(Status *stat)
Status current = *stat;
/* Iterate to accumulate. */
while (current.prev) {
/* Skip recursive status. */
if (StatusUtils_IsRecursive(current)) break;
current = *current.prev;
cnt += 1;
}

View File

@@ -43,13 +43,13 @@
/* Execute b whenever finds s is "NOT okay". */
# define notok(s, b) { Status _ = s; if (!StatusUtils_IsOkay(_)) b }
/* Return e when passing a failing e commented with c. */
# define fails(e, c) { notok(e, return annot(_, c);) }
/* Return e when passing a failing e. */
# define fail(e) { notok(e, return _;) }
/* Return v when passing a failing e. */
/* Return e when passing a failing e commented with c. */
# define fails(e, c) { notok(e, return annot(_, c);) }
/* Return value "v" when passing a failing e. */
# define vfail(e, v) { notok(e, return v;) }
/* Execute b for handling UnknownStatus (TraditionalFunctionReturn). */
@@ -123,6 +123,7 @@
.prev = (Status *)&p\
})
/* Create a new Status with v as its value. */
# define value(e, v) ((Status) {\
.identity = e.identity,\
.value = v,\
@@ -132,6 +133,17 @@
.prev = (Status *)e.prev\
})
/* Change the characteristic of e. */
# define shift(e, c) ((Status) {\
.identity = e.identity,\
.value = e.value,\
.description = e.description,\
.characteristic = c,\
.loc = e.loc,\
.prev= (Status *)e.prev\
})
/* Apply a new location to e where this macro is called. */
# define apply(e) ((Status) {\
.identity = e.identity,\
.value = e.value,\
@@ -141,7 +153,7 @@
.prev = (Status *)e.prev\
})
// Reannotate for e.
/* Replace the description from e with c. */
# define annot(e, c) ((Status) {\
.identity = e.identity,\
.value = e.value,\

38
test.c
View File

@@ -1,4 +1,3 @@
#include "Status/include/status.h"
#include <Compound/array.h>
#include <Compound/catlog.h>
#include <Compound/common.h>
@@ -14,13 +13,48 @@ Status func(void)
__attribute__((constructor))
void __CONSTRUCT__() {
cat("Hello, Compound!\n");
cat("Hello, Compound!");
}
__attribute__((destructor))
void __DESTRUCT__() {}
Status Main(void)
{
Memory mem;
fail(Memory_Create(&mem, sizeof(double)));
fail(Memory_Allocate(&mem));
(void)printf("%p:%ld\n", mem.addr, mem.size);
fail(Memory_Reallocate(&mem, sizeof(char)));
(void)printf("%p:%ld\n", mem.addr, mem.size);
fail(Memory_Release(&mem));
fail(Memory_Delete(&mem));
return apply(NormalStatus);
}
int tfunc(void)
{
vfail(apply(ErrorStatus), 1);
return 0;
}
Status MainMacroFailsTest(void)
{
unsure(apply(value(TraditionalFunctionReturn, tfunc())), _.value,
nest(_, __, fails(shift(__, STATUS_ERROR), "Failed on execution from tfunc.")));
return apply(NormalStatus);
}
Status MainArrayCreateAndDeleteWithModulatedMemberAccessing(void)
{
Array arr;
fail(Array_Create(&arr, 8, sizeof(int)));