$ pwd
/home/kaz/ustreamer
$ gcc -D_GNU_SOURCE -shared src/libs/base64.c -o base64.o
$ valgrind txr --free-all
==8785== Memcheck, a memory error detector
==8785== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==8785== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==8785== Command: txr --free-all
==8785==
This is the TXR Lisp interactive listener of TXR 292.
Quit with :quit or Ctrl-D on an empty line. Ctrl-X ? for cheatsheet.
Psst! The complimentary Allen key that comes with TXR is inpired by IKEA.
1> (with-dyn-lib "./base64.o"
(deffi us-base64-encode "us_base64_encode"
void (buf size-t (ptr (array 1 str-d)) (ptr (array 1 size-t)))))
us-base64-encode
2> (let ((out (vec nil))
(sz (vec 0)))
(us-base64-encode #b'00112233445566778899AABBCCDDEEFF' 16 out sz)
(list out sz))
(#("ABEiM0RVZneImaq7zN3u/w==") #(25))
3> (let ((out (vec "abcde"))
(sz (vec 5)))
(us-base64-encode #b'00112233445566778899AABBCCDDEEFF' 16 out sz)
(list out sz))
(#("ABEiM0RVZneImaq7zN3u/w==") #(25))
4> (let ((out (vec "xxxxxxxxxxxxxxxxxxxxxxxxxx"))
(sz (vec 25)))
(us-base64-encode #b'00112233445566778899AABBCCDDEEFF' 16 out sz)
(list out sz))
(#("ABEiM0RVZneImaq7zN3u/w==") #(25))
5> (let ((out (vec "xxxxxxxxxxxxxxxxxxxxxxxxxxxx"))
(sz (vec 27)))
(us-base64-encode #b'00112233445566778899AABBCCDDEEFF' 16 out sz)
(list out sz))
(#("ABEiM0RVZneImaq7zN3u/w==") #(27))
6>
==8785==
==8785== HEAP SUMMARY:
==8785== in use at exit: 0 bytes in 0 blocks
==8785== total heap usage: 24,782 allocs, 24,782 frees, 4,806,317 bytes allocated
==8785==
==8785== All heap blocks were freed -- no leaks are possible
==8785==
==8785== For counts of detected and suppressed errors, rerun with: -v
==8785== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
We covered the cases when the the destination buffer is already allocated, and smaller, equal to or in excess of the space being required. Thus testing the cases when realloc is or isn't necessary. No memory errors or leaks.
We can see in the last case that when the buffer is larger, the function leaves the size alone, returning our original 27.
We can see in the last case that when the buffer is larger, the function leaves the size alone, returning our original 27.