-
Notifications
You must be signed in to change notification settings - Fork 275
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
adds a stub for realloc #1035
adds a stub for realloc #1035
Changes from 4 commits
e647a29
bf31a28
73f9c13
8e74a24
2491fcd
90d5d18
e393461
d8080fe
a1ced40
c272546
659cb45
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,22 +29,45 @@ | |
|
||
(defun memory/allocate (ptr len) | ||
(if *malloc-initialize-memory* | ||
(memory-allocate ptr n *malloc-initial-value*) | ||
(memory-allocate ptr n))) | ||
(memory-allocate ptr len *malloc-initial-value*) | ||
(memory-allocate ptr len))) | ||
|
||
(defun word-size () (/ (word-width) 8)) | ||
|
||
(defun encode-memory-length(ptr len) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. lisp style issue, please add a space between the function name and the list of arguments. |
||
(write-word ptr_t ptr len)) | ||
|
||
(defun decode-memory-length(ptr) | ||
(read-word ptr_t ptr)) | ||
|
||
(defun decode-memory-length'(ptr) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I doubt that we need these two functions (again in the public interface).
|
||
(if ptr (decode-memory-length (- ptr (word-size))) 0)) | ||
|
||
(defun malloc (n) | ||
"allocates a memory region of size N" | ||
(declare (external "malloc")) | ||
(if (= n 0) *malloc-zero-sentinel* | ||
(if (malloc-will-reach-limit n) 0 | ||
(let ((n (+ n (* 2 *malloc-guard-edges*))) | ||
(let ((width (word-size)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. a) we always use the word b) please choose variable names that bear some semantics, instead of using generic
|
||
(full (+ n (* 2 *malloc-guard-edges*) width)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. rename |
||
(ptr brk) | ||
(failed (memory/allocate ptr n))) | ||
(failed (memory/allocate ptr full))) | ||
(if failed 0 | ||
(set brk (+ brk n)) | ||
(malloc/fill-edges ptr n) | ||
(+ ptr *malloc-guard-edges*)))))) | ||
|
||
(set brk (+ brk full)) | ||
(malloc/fill-edges ptr full) | ||
(set ptr (+ ptr *malloc-guard-edges*)) | ||
(encode-memory-length ptr n) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. rename |
||
(+ ptr width)))))) | ||
|
||
(defun realloc (ptr new) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please, rewrite this function to conform with POSIX. In particular,
(defun realloc (ptr len)
(or (and (not ptr) (malloc ptr len))
(and (not len) (free ptr))
(let ((old-len (malloc/get-chunk-size ptr)))
(if (<= old-len len)
(prog (malloc/put-chunk-size ptr len) ptr)
(free ptr)
(let (new-ptr (malloc ptr len))
(if new-ptr (memcpy new-ptr ptr old-len)
new-ptr)))))) Note, if There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This |
||
(declare (external "realloc")) | ||
(let ((old (decode-memory-length' ptr)) | ||
(ptr' (malloc new)) | ||
(dst ptr')) | ||
(if (not ptr) ptr' | ||
(when (and ptr ptr') | ||
(copy-right dst ptr old)) | ||
ptr'))) | ||
|
||
;; in our simplistic malloc implementation, free is just a nop | ||
(defun free (p) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,9 @@ | |
(defmethod call (name ptr) | ||
(when (and ptr (= name 'free) | ||
(not (= ptr *malloc-zero-sentinel*))) | ||
(when (and ptr | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
So the code will be now much more readable: (defmethod call (name ptr)
(when (and ptr (= name 'free)
(not (= ptr *malloc-zero-sentinel*)))
(unless (memcheck-is-allocated 'malloc ptr)
(memcheck-acquire 'malloc ptr 1))
(memcheck-release 'malloc ptr)))
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, I was really confused yesterday :) (defmacro when (cnd body)
"(when CND BODY) if CND evaluates to true, then BODY is evaluated and
the value of the last expression in BODY becomes the value of the
whole expression. Otherwise, if CND evaluates to false, nil is returned."
(if cnd (prog body) ()))
(defmacro unless (cnd body)
"(unless CND BODY) if CND evaluates to false, then BODY is evaluated and
the value of the last expression in BODY becomes the value of the
whole expression. Otherwise, if CND evaluates to true, nil is returned."
(if (not cnd) () body))
(defmacro until (c b)
"(until COND BODY) if COND evaluates to true, then the whole expression
evaluates to nil and BODY is not evaluated. Otherwise, if COND evaluates
to false, then BODY is evaluated until COND evaluates to true and the value
of the last evaluation of BODY becomes the value of the whole expression."
(while (not c) b)) |
||
(not (memcheck-is-allocated 'malloc ptr))) | ||
(memcheck-acquire 'malloc ptr 1)) | ||
(memcheck-release 'malloc ptr))) | ||
|
||
(defmethod loaded (ptr) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,12 @@ | |
(require incident) | ||
|
||
;; Public Interface | ||
|
||
(defun memcheck-is-allocated (heap ptr) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would name it |
||
(or | ||
(region-contains (symbol-concat 'memcheck/live heap) ptr) | ||
(region-contains (symbol-concat 'memcheck/dead heap) ptr))) | ||
|
||
(defun memcheck-release (heap ptr) | ||
(let ((dead (region-contains (symbol-concat 'memcheck/dead heap) ptr))) | ||
(if dead (memcheck/report-double-release ptr) | ||
|
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.
adding this function to the public interface will introduce confusion, between word-size and word-width so, please either remove it or mangle correspondingly.