-
Notifications
You must be signed in to change notification settings - Fork 0
/
mem.c
63 lines (54 loc) · 1.17 KB
/
mem.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/*
*
* mem.c
* (Memory)
* Functions to help with memory manipulation.
*
*/
/* SMPL headers */
#include "smpl.h"
#include "error.h"
#include "state.h"
/* C headers */
#include <stdlib.h>
/*
** Allocate an memory buffer with the given size.
** Returns NULL if the given size is equal to 0.
** In case of allocation failure,
** SMPL throws an "Out of memory" error
*/
void *spM_alloc (sp_State *S, uint size)
{
if(size == 0)
return NULL;
void *block = malloc(size);
if(block == NULL)
sp_error_throw(S, S->ln, EC_OUT_MEM);
return block;
}
/*
** If the vector is NOT admissible
** (it's top index is equal to it's capacity)
** then PUSH an error with message 'err' on line 'ln'
*/
uint spM_check_admissible (sp_State *S, uint top, uint cap, uint ln, sp_ErrorCode ec)
{
if(top == cap)
{
sp_error_push(S, ln, ec);
return 0;
}
return 1;
}
/*
** If the vector is NOT admissible
** (it's top index is equal to it's capacity)
** then THROW an error with message 'err' on line 'ln'
*/
void spM_assert_admissible (sp_State *S, uint top, uint cap, uint ln, sp_ErrorCode ec)
{
if(top == cap)
{
sp_error_throw(S, ln, ec);
}
}