Skip to content

Commit 26fd49f

Browse files
committed
optimized the zero filling in calloc (__TICE__ only)
1 parent 46dd9fe commit 26fd49f

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

src/libc/allocator.src

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,49 @@
44
public _malloc, _free, _realloc
55

66
public _calloc
7+
8+
if defined __TICE__
9+
10+
; uses the hardware specific $E40000 memory location
11+
12+
; void *calloc(size_t nmemb, size_t size)
13+
_calloc:
14+
pop de
15+
pop bc
16+
ex (sp), hl
17+
push bc
18+
push de
19+
call __imulu
20+
push hl
21+
push hl
22+
call _malloc
23+
pop bc ; reset SP
24+
; test for NULL
25+
add hl, bc
26+
or a, a
27+
sbc hl, bc
28+
pop bc ; BC = size
29+
ret z ; return NULL
30+
; inlined bzero
31+
push hl
32+
ex de, hl ; DE = dest
33+
; test if the size is zero
34+
scf
35+
sbc hl, hl
36+
add hl, bc
37+
jr nc, .finish
38+
; large region of all zeros on the Ti84CE
39+
ld hl, $E40000 ; HL = src
40+
ldir
41+
.finish:
42+
pop hl ; return value
43+
ret
44+
45+
else
46+
47+
; makes no hardware assumptions
48+
49+
; void *calloc(size_t nmemb, size_t size)
750
_calloc:
851
pop de
952
pop bc
@@ -27,6 +70,8 @@ _calloc:
2770
pop de
2871
ret
2972

73+
end if
74+
3075
if defined ALLOCATOR_SIMPLE
3176

3277
_malloc := __simple_malloc

test/standalone/asprintf_fprintf/src/main.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,12 @@ int memccpy_tests(void) {
418418
return __LINE__;
419419
}
420420

421+
/* check that no crashes occur with small calloc sizes */
422+
buf = (char*)calloc(1, sizeof(char));
423+
free(buf);
424+
buf = (char*)calloc(0, sizeof(char));
425+
free(buf);
426+
421427
buf = (char*)calloc(file_size + 1, sizeof(char));
422428
if (buf == NULL) {
423429
perror("calloc failure");

0 commit comments

Comments
 (0)