home *** CD-ROM | disk | FTP | other *** search
/ ARM Club 1 / ARM_CLUB_CD.iso / contents / apps / clib / progs / stubshack / StubsHack_h_StubsHack < prev    next >
Encoding:
Text File  |  1995-01-07  |  5.4 KB  |  167 lines

  1. #ifndef __StubsHack_h
  2. #define __StubsHack_h
  3.  
  4. #include <stdlib.h>
  5.  
  6. #ifndef BOOL
  7. #define BOOL  unsigned
  8. #define FALSE 0
  9. #define TRUE  1
  10. #endif
  11.  
  12.  
  13. typedef void (*StubsHack_fnptr)( void);
  14.     /* A generic pointer to a function.    */
  15.  
  16. typedef void *(*StubsHack_mallocfn)    ( size_t);
  17. typedef void *(*StubsHack_reallocfn)    ( void *, size_t);
  18. typedef void *(*StubsHack_callocfn)    ( size_t, size_t);
  19. typedef void  (*StubsHack_freefn)    ( void *);
  20.     /* Some function-pointer types...    */
  21.  
  22.  
  23. extern StubsHack_mallocfn    StubsHack_scl_malloc;
  24. extern StubsHack_freefn        StubsHack_scl_free;
  25. extern StubsHack_reallocfn    StubsHack_scl_realloc;
  26. extern StubsHack_callocfn    StubsHack_scl_calloc;
  27.     /* These will always point to the vanilla shared c lib functions.    */
  28.     /* They will initially point to malloc etc in stubs. After a succesful    */
  29.     /* call to StubsHack_ReplaceANSIAllocFns, they will be modified to     */
  30.     /* point directly to the functions in the shared c library, because the    */
  31.     /* stubs functions will point elsewhere...                */
  32.  
  33.  
  34. typedef enum    {
  35.     StubsHack_error_OK = 0,
  36.     StubsHack_error_NOTSTUBS
  37.     }
  38.     StubsHack_error;
  39.  
  40.  
  41. StubsHack_error    StubsHack_RedirectStubsFn(
  42.         StubsHack_fnptr    stubs_fn,
  43.         StubsHack_fnptr    replacement_fn,
  44.         StubsHack_fnptr    *old_fn_store
  45.         );
  46.     /*
  47.     Replaces a single stubs function 'stubs_fn'. All subsequent calls to
  48.     'stubs_fn' go to 'replacement_fn'. The actual SharedCLibrary function
  49.     which implements 'stubs_fn' is put into '*old_fn_store' (if this isn't
  50.     NULL), which you should use if 'replacement_fn' is just a wrapper function.
  51.     The redirection will not take place if 'replacement_fn' is NULL, but 
  52.     'old_fn_store ' will still be set.
  53.     
  54.     Most stubs functions aren't 'void foo( void)', so you will probably
  55.     need to cast the first two arguments to this function to type
  56.     (StubsHack_fnptr), and the last to (StubsHack_fnptr *).
  57.     */
  58.  
  59.  
  60.  
  61. StubsHack_error    StubsHack_ReplaceANSIAllocFns(
  62.         StubsHack_mallocfn    new_malloc,
  63.         StubsHack_reallocfn    new_realloc,
  64.         StubsHack_callocfn    new_calloc,
  65.         StubsHack_freefn    new_free,
  66.  
  67.         StubsHack_mallocfn    *old_malloc_store,
  68.         StubsHack_reallocfn    *old_realloc_store,
  69.         StubsHack_callocfn    *old_calloc_store,
  70.         StubsHack_freefn    *old_free_store,
  71.  
  72.         BOOL            make_stack_allocater_be_mallocfree
  73.         );
  74. /*
  75.  
  76. Allows total replacement of the ANSI heap management functions. The
  77. first four parameters are your replacement functions. (Use NULL to leave
  78. a stubs function unchanged.). The next four params are addresses of
  79. pointers to functions which, if not NULL, will be filled in to point to
  80. the previous re/c/malloc/free functions. Thus your 'new_malloc'
  81. functions could call 'old_malloc_store' after doing some error-checking
  82. etc, making it a 'wrapper' function.
  83.  
  84. The first time 'StubsHack_ReplaceANSIAllocFns' is called, it sets the
  85. global variables StubsHack_scl_malloc/realloc/calloc/free to point to
  86. the original destination of malloc/etc. This enables you to always have
  87. access to the vanilla SharedCLib functions, and also enables the use of
  88. _kernel_register_allocs as follows:
  89.  
  90. If 'make_stack_allocaters_be_mallocfree' is TRUE,
  91. _kernel_register_allocs will be called with the addresses of malloc/free
  92. in the shared c library (using StubsHack_scl_malloc etc), so that stack
  93. chunks will be allocated using the standard C malloc/free functions.
  94. This is the best option, as the functions which allocate stack chunks
  95. mustn't use more than 41 words of stack and mustn't check the stack.
  96.  
  97. If 'make_stack_allocaters_be_mallocfree' is FALSE, then
  98. _kernel_register_allocs will not be called, so (assuming that
  99. the stack allocators are malloc and free before
  100. StubsHack_ReplaceANSIAllocFns is called) new_malloc and new_free will be
  101. used to allocate stack henceforth, which is only ok if they are
  102. functions which don't check the stack and use less than 41 words of
  103. stack.
  104.  
  105. The only problem I can forsee is if malloc was used before this
  106. function, in which case your 'new_free' function will be called with
  107. a pointer that was allocated using the shared c library malloc,
  108. rather than your 'new_malloc' function. Hence call
  109. StubsHack_ReplaceANSIAllocFns asap in your prog.
  110.  
  111. Note that 'StubsHack_ReplaceANSIAllocFns' can be called repeatedly,
  112. diverting malloc to different functions. When malloc is actually used,
  113. these functions will call each other in a nested fasion, as long as each
  114. one uses what it thinks is the original malloc function - ie. non of
  115. them actually replace malloc completely.
  116.  
  117.  
  118. */
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127. /* The stuff below here is probably not much use...    */
  128. /* 'tis used internally though.                */
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136. StubsHack_fnptr    StubsHack_GetDestOfB( StubsHack_fnptr address);
  137.     /* Returns the address in the shared c lib of the function 'address'    */
  138.     /* I think this will only work properly when the branch is a forward    */
  139.     /* one. (ok for branches from stubs into the shared c lib        */
  140.     /* Returns NULL if the function isn't a stubs function, ie. isn't a     */
  141.     /* simple ARM branch instruction.                    */
  142.  
  143.  
  144.  
  145.  
  146.  
  147. #define StubsHack_MakeBranchInstruction( instr, dest)                \
  148.     *((int *) ((int) (instr))) =                         \
  149.         0xEA000000                             \
  150.         |                                \
  151.         (                                \
  152.             ((((int) (dest)) - ((int) (instr)) - 8) / 4)        \
  153.             & 0x00FFFFFF                        \
  154.         )                                \
  155.     /* Makes the word at address 'instr' be a simple branch to address    */
  156.     /* 'destination'                            */
  157.     /* i.e. top byte is 0xEA, and bottom 3 bytes are offset (in words) of    */
  158.     /* 'dest' from 'instr'.                            */
  159.     /* This is a macro 'cos we don't want too much chance of stack         */
  160.     /* extensions (caused by fn calls) while malloc etc are being changed.    */
  161.     /* Actually, _kernel_register_allocs is called now so we should be ok.    */
  162.  
  163.  
  164.  
  165.  
  166. #endif
  167.