Skip to content

Commit 98c1a3e

Browse files
committed
Implement helper function for dynamic arrays
1 parent 33718aa commit 98c1a3e

File tree

5 files changed

+34
-27
lines changed

5 files changed

+34
-27
lines changed

Diff for: .clang-format

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ ConstructorInitializerAllOnOneLineOrOnePerLine: false
2525
ConstructorInitializerIndentWidth: 4
2626
DerivePointerAlignment: false
2727
ExperimentalAutoDetectBinPacking: false
28-
IndentCaseLabels: false
28+
IndentCaseLabels: true
2929
IndentWrappedFunctionNames: false
3030
IndentFunctionDeclarationAfterType: false
3131
MaxEmptyLinesToKeep: 1
@@ -47,7 +47,7 @@ Standard: Cpp11
4747
IndentWidth: 4
4848
TabWidth: 8
4949
UseTab: Never
50-
BreakBeforeBraces: Attach
50+
BreakBeforeBraces: Linux
5151
SpacesInParentheses: false
5252
SpacesInSquareBrackets: false
5353
SpacesInAngles: false

Diff for: CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ set(CMAKE_C_COMPILER "/usr/bin/clang")
66
set(CMAKE_C_STANDARD 11)
77
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g -O0 -Wall -Wextra -fsanitize=address -fno-omit-frame-pointer")
88

9-
set(SOURCE_FILES main.c lex.c lex.h run.h run.c parse.c parse.h scope.c scope.h compile.c compile.h)
9+
set(SOURCE_FILES main.c lex.c lex.h run.h run.c parse.c parse.h scope.c scope.h compile.c compile.h array-util.h)
1010
add_executable(PHPInterp ${SOURCE_FILES})

Diff for: array-util.h

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#ifndef PHPINTERP_ARRAY_UTIL_H
2+
#define PHPINTERP_ARRAY_UTIL_H
3+
4+
#include <stddef.h>
5+
#include <stdlib.h>
6+
#include <memory.h>
7+
8+
static void try_resize(size_t* capacity, size_t size, void** ptr,
9+
size_t elemsize, void (*error)(char*))
10+
{
11+
if (*capacity < size + 1) {
12+
*capacity *= 2;
13+
void* tmp = realloc(*ptr, elemsize * (*capacity));
14+
if (!tmp) {
15+
error("Out of memory");
16+
return;
17+
}
18+
19+
*ptr = tmp;
20+
}
21+
}
22+
23+
#endif //PHPINTERP_ARRAY_UTIL_H

Diff for: run.c

+3-11
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "parse.h"
99
#include "scope.h"
1010
#include "compile.h"
11+
#include "array-util.h"
1112

1213
Variant cpy_var(Variant var)
1314
{
@@ -69,17 +70,8 @@ static void destroy_runtime(Runtime* R)
6970

7071
static inline void try_stack_resize(Runtime* R)
7172
{
72-
if (R->stackcapacity < R->stacksize+1) {
73-
R->stackcapacity *= 2;
74-
Variant* tmp = realloc(R->stack, sizeof(*R->stack) * R->stackcapacity);
75-
if (!tmp) {
76-
runtimeerror("Out of memory");
77-
return;;
78-
}
79-
80-
memset(&tmp[R->stacksize], 0, R->stackcapacity - R->stacksize);
81-
R->stack = tmp;
82-
}
73+
try_resize(&R->stackcapacity, R->stacksize, (void*)&R->stack,
74+
sizeof(*R->stack), runtimeerror);
8375
}
8476

8577
static void push(Runtime* R, Variant val)

Diff for: scope.c

+5-13
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include <memory.h>
22
#include <stdlib.h>
33
#include "scope.h"
4+
#include "array-util.h"
45

56
static const Variant undefined = {.type = UNDEFINED};
67

@@ -36,19 +37,10 @@ Variant lookup(Runtime *R, char *str)
3637
return undefined;
3738
}
3839

39-
static void try_resize(Scope* scope)
40+
static void try_vars_resize(Scope* scope)
4041
{
41-
if (scope->capacity < scope->size+1) {
42-
scope->capacity *= 2;
43-
Variable* tmp = realloc(scope->vars, sizeof(*scope->vars) * scope->capacity);
44-
if (!tmp) {
45-
runtimeerror("Out of memory");
46-
return;
47-
}
48-
49-
memset(&tmp[scope->size], 0, scope->capacity - scope->size);
50-
scope->vars = tmp;
51-
}
42+
try_resize(&scope->capacity, scope->size, (void*)&scope->vars,
43+
sizeof(*scope->vars), runtimeerror);
5244
}
5345

5446
Variable* set_var(Runtime* R, char* str, Variant var)
@@ -63,7 +55,7 @@ Variable* set_var(Runtime* R, char* str, Variant var)
6355
}
6456
}
6557

66-
try_resize(R->scope);
58+
try_vars_resize(R->scope);
6759
const size_t idx = R->scope->size++;
6860
vars[idx].name = strdup(str);
6961
vars[idx].value = cpy_var(var);

0 commit comments

Comments
 (0)