-
-
Notifications
You must be signed in to change notification settings - Fork 63
changed behaviour of malloc(0) and optimized calloc #631
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
ZERICO2005
wants to merge
3
commits into
master
Choose a base branch
from
opt_calloc
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,60 +1,40 @@ | ||
assume adl=1 | ||
|
||
section .text | ||
public _malloc, _free, _realloc | ||
|
||
public _calloc | ||
_calloc: | ||
pop de | ||
pop bc | ||
ex (sp),hl | ||
push bc | ||
push de | ||
call __imulu | ||
push hl | ||
push hl | ||
call _malloc | ||
pop de | ||
add hl,de | ||
xor a,a | ||
sbc hl,de | ||
ld e,a | ||
push de | ||
push hl | ||
call nz,_memset | ||
pop de | ||
pop de | ||
pop de | ||
ret | ||
|
||
public _malloc, _free, _realloc, _calloc | ||
|
||
if defined ALLOCATOR_SIMPLE | ||
|
||
_malloc := __simple_malloc | ||
_free := __simple_free | ||
_realloc := __simple_realloc | ||
extern __simple_free | ||
extern __simple_malloc | ||
extern __simple_realloc | ||
_calloc := __nonzero_fill_calloc | ||
extern __simple_free | ||
extern __simple_malloc | ||
extern __simple_realloc | ||
|
||
else if defined ALLOCATOR_STANDARD | ||
|
||
_malloc := __standard_malloc | ||
_free := __standard_free | ||
_realloc := __standard_realloc | ||
extern __standard_malloc | ||
extern __standard_free | ||
extern __standard_realloc | ||
_calloc := __nonzero_fill_calloc | ||
extern __standard_malloc | ||
extern __standard_free | ||
extern __standard_realloc | ||
|
||
else ; custom functions provided by the program | ||
|
||
_malloc := __custom_malloc | ||
_free := __custom_free | ||
_realloc := __custom_realloc | ||
extern __custom_malloc | ||
extern __custom_free | ||
extern __custom_realloc | ||
_calloc := __generic_calloc | ||
extern __custom_malloc | ||
extern __custom_free | ||
extern __custom_realloc | ||
|
||
end if | ||
|
||
extern __imulu | ||
extern _memset | ||
extern __nonzero_fill_calloc | ||
extern __generic_calloc |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
assume adl=1 | ||
|
||
; __nonzero_fill_calloc assumes that malloc(0) returns NULL | ||
; __generic_calloc makes no assumptions | ||
|
||
;------------------------------------------------------------------------------- | ||
|
||
section .text | ||
|
||
public __generic_calloc | ||
|
||
if defined __TICE__ | ||
|
||
; uses TICE specific hardware/platform optimizations | ||
|
||
; void *calloc(size_t nmemb, size_t size) | ||
__generic_calloc: | ||
pop de | ||
pop bc | ||
ex (sp), hl | ||
push bc | ||
push de | ||
call __imulu | ||
push hl | ||
push hl | ||
call _malloc | ||
pop bc ; reset SP | ||
pop bc ; BC = size | ||
; test for NULL | ||
add hl, bc | ||
; or a, a ; assumes that ptr + size does not overflow on TICE | ||
sbc hl, bc | ||
ret z ; return NULL | ||
; inlined bzero | ||
push hl | ||
ex de, hl ; DE = dest | ||
; test if the size is zero | ||
scf | ||
sbc hl, hl | ||
add hl, bc | ||
jr nc, .finish | ||
; large region of all zeros on the Ti84CE | ||
ld hl, $E40000 ; HL = src | ||
ldir | ||
pop hl ; return value | ||
ret | ||
|
||
else | ||
|
||
; makes no hardware assumptions | ||
|
||
; void *calloc(size_t nmemb, size_t size) | ||
__generic_calloc: | ||
pop de | ||
pop bc | ||
ex (sp), hl | ||
push bc | ||
push de | ||
call __imulu | ||
push hl | ||
push hl | ||
call _malloc | ||
pop bc ; reset SP | ||
pop bc ; BC = size | ||
; test for NULL | ||
add hl, bc | ||
xor a, a | ||
sbc hl, bc | ||
ret z ; return NULL | ||
; inlined memset/bzero | ||
cpi | ||
add hl, bc | ||
ret c ; size is zero | ||
dec hl | ||
ld (hl), a | ||
ret po ; size is one | ||
push hl | ||
pop de | ||
dec de | ||
lddr | ||
ret | ||
|
||
end if | ||
|
||
;------------------------------------------------------------------------------- | ||
|
||
section .text | ||
|
||
public __nonzero_fill_calloc | ||
|
||
if defined __TICE__ | ||
|
||
; uses TICE specific hardware/platform optimizations | ||
|
||
; void *calloc(size_t nmemb, size_t size) | ||
__nonzero_fill_calloc: | ||
pop de | ||
pop bc | ||
ex (sp), hl | ||
push bc | ||
push de | ||
call __imulu | ||
push hl | ||
push hl | ||
call _malloc | ||
pop bc ; reset SP | ||
pop bc ; BC = size | ||
; test for NULL | ||
add hl, bc | ||
; or a, a ; assumes that ptr + size does not overflow on TICE | ||
sbc hl, bc | ||
ret z ; return NULL | ||
; inlined bzero | ||
; assumes that malloc(0) returns NULL, so we can skip the check for zero size | ||
push hl | ||
ex de, hl ; DE = dest | ||
; large region of all zeros on the Ti84CE | ||
ld hl, $E40000 ; HL = src | ||
ldir | ||
pop hl ; return value | ||
ret | ||
|
||
else | ||
|
||
; makes no hardware assumptions | ||
|
||
; void *calloc(size_t nmemb, size_t size) | ||
__nonzero_fill_calloc: | ||
pop de | ||
pop bc | ||
ex (sp), hl | ||
push bc | ||
push de | ||
call __imulu | ||
push hl | ||
push hl | ||
call _malloc | ||
pop bc ; reset SP | ||
pop bc ; BC = size | ||
; test for NULL | ||
add hl, bc | ||
xor a, a | ||
sbc hl, bc | ||
ret z ; return NULL | ||
; inlined memset/bzero | ||
; assumes that malloc(0) returns NULL, so we can skip the check for zero size | ||
add hl, bc | ||
cpd | ||
ld (hl), a | ||
ret po ; size is one | ||
push hl | ||
pop de | ||
dec de | ||
lddr | ||
ret | ||
|
||
end if | ||
|
||
;------------------------------------------------------------------------------- | ||
|
||
extern __imulu | ||
extern _malloc | ||
extern _memset |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I assume it is okay for
cpd
to readnonnull_ptr_from_malloc + size