From 972288e62fa0798f59caf209ab58c911d289a8b2 Mon Sep 17 00:00:00 2001 From: Denis Vlasenko Date: Thu, 15 Mar 2007 00:57:01 +0000 Subject: modify ptr_to_globals trick so that we do not violate type safety (well, sort of ;)) --- docs/keep_data_small.txt | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) (limited to 'docs/keep_data_small.txt') diff --git a/docs/keep_data_small.txt b/docs/keep_data_small.txt index 272aa21c5..ec13b4d3f 100644 --- a/docs/keep_data_small.txt +++ b/docs/keep_data_small.txt @@ -59,30 +59,33 @@ archival/libunarchive/decompress_unzip.c: This example completely eliminates globals in that module. Required memory is allocated in inflate_gunzip() [its main module] -and then passed down to all subroutines which need to access globals +and then passed down to all subroutines which need to access 'globals' as a parameter. Example 2 In case you don't want to pass this additional parameter everywhere, take a look at archival/gzip.c. Here all global data is replaced by -singe global pointer (ptr_to_globals) to allocated storage. +single global pointer (ptr_to_globals) to allocated storage. In order to not duplicate ptr_to_globals in every applet, you can reuse single common one. It is defined in libbb/messages.c -as void *ptr_to_globals, but is NOT declared in libbb.h. -You first define a struct: +as struct globals *ptr_to_globals, but the struct globals is +NOT defined in libbb.h. You first define your own struct: -struct my_globals { int a; char buf[1000]; }; +struct globals { int a; char buf[1000]; }; and then declare that ptr_to_globals is a pointer to it: -extern struct my_globals *ptr_to_globals; #define G (*ptr_to_globals) -Linker magic enures that these two merge into single pointer object. +Linker magic ensures that these two merge into single pointer object. Now initialize it in _main(): ptr_to_globals = xzalloc(sizeof(G)); and you can reference "globals" by G.a, G.buf and so on, in any function. + +The drawback is that now you have to initialize it by hand. xzalloc() +can be helpful in clearing allocated storage to 0, but anything more +must be done by hand. -- cgit v1.2.3