Skip to content

Commit f8d9500

Browse files
committed
reimplemented custom allocator
1 parent c1210ce commit f8d9500

4 files changed

Lines changed: 49 additions & 6 deletions

File tree

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
.assume adl=1
2+
3+
.section .text._malloc
4+
.global _malloc
5+
.type _malloc, @function
6+
_malloc:
7+
jp __custom_malloc
8+
9+
.section .text._free
10+
.global _free
11+
.type _free, @function
12+
_free:
13+
jp __custom_free
14+
15+
.section .text._realloc
16+
.global _realloc
17+
.type _realloc, @function
18+
_realloc:
19+
jp __custom_realloc
20+
21+
.extern __custom_malloc
22+
.extern __custom_free
23+
.extern __custom_realloc

src/libc/allocator/allocator_simple.src

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
.assume adl=1
22

3-
.section .text._malloc
3+
.section .text
44

55
.global _malloc
66
.type _malloc, @function

src/libc/allocator/makefile

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ include $(CURDIR)/../../common.mk
1818

1919
.SECONDARY:
2020

21-
all: build/allocator_standard.a build/allocator_simple.a
21+
all: build/allocator_standard.a build/allocator_simple.a build/allocator_custom.a
2222

2323
# --- allocator_simple
2424

@@ -44,12 +44,23 @@ build/allocator_standard.src: allocator_standard.c
4444
$(Q)$(call MKDIR,build)
4545
$(Q)$(EZCC) $(EZCFLAGS) $< -o $@
4646

47+
# --- allocator_custom
48+
49+
build/allocator_custom.a: build/allocator_custom.o
50+
$(Q)$(call MKDIR,build)
51+
$(Q)$(EZAR) rcs $@ $^
52+
53+
build/allocator_custom.o: allocator_custom.src
54+
$(Q)$(call MKDIR,build)
55+
$(Q)$(EZAS) $(EZASFLAGS) $< -o $@
56+
4757
clean:
4858
$(Q)$(call RMDIR,build)
4959

5060
install: all
5161
$(Q)$(call MKDIR,$(INSTALL_LIBC))
5262
$(Q)$(call COPY,$(call NATIVEPATH,build/allocator_simple.a),$(INSTALL_LIBC))
5363
$(Q)$(call COPY,$(call NATIVEPATH,build/allocator_standard.a),$(INSTALL_LIBC))
64+
$(Q)$(call COPY,$(call NATIVEPATH,build/allocator_custom.a),$(INSTALL_LIBC))
5465

5566
.PHONY: all clean install

src/makefile.mk

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,18 @@ LINKER_SCRIPT ?= $(call FORWARD_PATH,$(CEDEV_TOOLCHAIN)/meta/linker_script.ld)
130130
# allocator (malloc/realloc/free)
131131
ifeq ($(ALLOCATOR),STANDARD)
132132
LIB_ALLOCATOR = $(call FORWARD_PATH,$(CEDEV_TOOLCHAIN)/lib/libc/allocator_standard.a)
133-
endif
134-
ifeq ($(ALLOCATOR),SIMPLE)
133+
else ifeq ($(ALLOCATOR),SIMPLE)
135134
LIB_ALLOCATOR = $(call FORWARD_PATH,$(CEDEV_TOOLCHAIN)/lib/libc/allocator_simple.a)
135+
else ifeq ($(ALLOCATOR),CUSTOM)
136+
LIB_ALLOCATOR = $(call FORWARD_PATH,$(CEDEV_TOOLCHAIN)/lib/libc/allocator_custom.a)
137+
else
138+
$(error ALLOCATOR must be one of STANDARD, SIMPLE, or CUSTOM)
139+
endif
140+
141+
ifeq ($(ALLOCATOR),CUSTOM)
142+
CUSTOM_ALLOCATOR_SYMBOLS = --undefined __custom_malloc --undefined __custom_free --undefined __custom_realloc
143+
else
144+
CUSTOM_ALLOCATOR_SYMBOLS =
136145
endif
137146

138147
# ensure always a hexadecimal value
@@ -481,7 +490,7 @@ $(OBJDIR)/%.$(CPP_EXTENSION).bc: $$(call UPDIR_RM,$$*).$(CPP_EXTENSION) $(EXTRA_
481490
$(Q)$(CC) -MD -c -emit-llvm $(EZCXXFLAGS) $(call QUOTE_ARG,$<) -o $(call QUOTE_ARG,$@)
482491

483492
# crt
484-
$(OBJDIR)/$(TARGETTMP): $(OBJECTS) $(LIB_ALLOCATOR) $(LIB_PRINTF) $(LIB_CXX) $(LIB_CE) $(LIB_SOFTFLOAT) $(LIB_CRT) $(LIB_C) $(ICON_OBJ) $(EXTRA_LIBS) $(MAKEFILE_LIST) $(DEPS)
493+
$(OBJDIR)/$(TARGETTMP): $(OBJECTS) $(LIB_PRINTF) $(LIB_CXX) $(LIB_CE) $(LIB_SOFTFLOAT) $(LIB_CRT) $(LIB_C) $(ICON_OBJ) $(EXTRA_LIBS) $(MAKEFILE_LIST) $(DEPS)
485494
$(Q)$(call MKDIR,$(@D))
486495
$(Q)$(LD) \
487496
-i \
@@ -491,6 +500,7 @@ $(OBJDIR)/$(TARGETTMP): $(OBJECTS) $(LIB_ALLOCATOR) $(LIB_PRINTF) $(LIB_CXX) $(L
491500
--gc-sections \
492501
--omagic \
493502
--defsym __TICE__=1 \
503+
$(CUSTOM_ALLOCATOR_SYMBOLS) \
494504
$(SPRINTF_SYMBOL) \
495505
$(LD_DEBUG) \
496506
$(EXTRA_PRE_LDFLAGS) \
@@ -499,7 +509,6 @@ $(OBJDIR)/$(TARGETTMP): $(OBJECTS) $(LIB_ALLOCATOR) $(LIB_PRINTF) $(LIB_CXX) $(L
499509
$(EXTRA_LIBS) \
500510
--whole-archive $(LIB_PRINTF) --no-whole-archive \
501511
--start-group \
502-
$(LIB_ALLOCATOR) \
503512
$(LIB_CRT) \
504513
$(LIB_CE) \
505514
$(LIB_SOFTFLOAT) \

0 commit comments

Comments
 (0)