From d4ddec9e127160ea498d8364d8c060882c035175 Mon Sep 17 00:00:00 2001 From: Olivier Gayot Date: Fri, 14 Feb 2014 03:23:14 +0100 Subject: add a function which clears a list list_clear(&plist) the function will run through every elements and clear them. the prehead is updated (first and size are set to 0) --- list.asm | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/list.asm b/list.asm index a463c8e..50c86b3 100644 --- a/list.asm +++ b/list.asm @@ -21,6 +21,7 @@ ;; THE SOFTWARE. extern malloc +extern free ;; initializes the list stored at address $rdi with default values global list_init:function @@ -32,6 +33,10 @@ global list_new_raw:function ;; same as list_new_raw, unless the list is initialized with default values global list_new:function +;; remove every element of the list pointed to by $rip. +;; the elements are freed using free() from the libc +global list_clear:function + ;; create a new element (using dynamic allocation) at the end of the list ;; stored at address $rdi. the created element will contain the value of $rsi ;; if, unlikely, the allocation fails, $rax will contain -1. @@ -86,6 +91,41 @@ list_new_raw: ;; {{{ leave ret +;; }}} +list_clear: ;; {{{ + + enter 0, 0 + + mov ecx, [rdi + list_t.size] ; load the size of the list into the counter + + test ecx, ecx + je .end + + mov rsi, QWORD [rdi + list_t.first] + + ; the list is reset to its initial state + mov DWORD [rdi + list_t.size], 0 + mov QWORD [rdi + list_t.first], 0 + + .loop: + push QWORD [rsi + elem_t.next] + push rcx + + mov rdi, rsi + call free + + pop rcx + pop rsi + + dec ecx + + test ecx, ecx + jne .loop + + .end + leave + ret + ;; }}} list_append: ;; {{{ -- cgit v1.2.3