Main Page | Modules | Directories | File List | Globals | Related Pages

alloc.c

Go to the documentation of this file.
00001 #ifndef _ALLOC_C_
00002 #define _ALLOC_C_
00003 
00004 /* -*- linux-c -*- */
00005 /** @file alloc.c
00006  * @brief Memory functions.
00007  */
00008 /** @addtogroup alloc Memory Functions
00009  * Basic malloc/calloc/free functions. These will be changed so 
00010  * that memory allocation errors will call a handler.  The default will
00011  * send a signal to the user-space daemon that will trigger the module to
00012  * be unloaded.
00013  * @todo Need error handling for memory allocations
00014  * @todo Some of these currently use kmalloc (GFP_ATOMIC) for
00015  * small allocations.  This should be evaluated for performance
00016  * and stability.
00017  * @{
00018  */
00019 
00020 enum errorcode { ERR_NONE=0, ERR_NO_MEM };
00021 enum errorcode _stp_error = ERR_NONE;
00022 
00023 /** Allocates memory within a probe.
00024  * This is used for small allocations from within a running
00025  * probe where the process cannot sleep. 
00026  * @param len Number of bytes to allocate.
00027  * @return a valid pointer on success or NULL on failure.
00028  * @bug Currently uses kmalloc (GFP_ATOMIC).
00029  */
00030 
00031 void *_stp_alloc(size_t len)
00032 {
00033         void *ptr = kmalloc(len, GFP_ATOMIC);
00034         if (unlikely(ptr == NULL))
00035                 _stp_error = ERR_NO_MEM;
00036         return ptr;
00037 }
00038 
00039 /** Allocates and clears memory within a probe.
00040  * This is used for small allocations from within a running
00041  * probe where the process cannot sleep. 
00042  * @param len Number of bytes to allocate.
00043  * @return a valid pointer on success or NULL on failure.
00044  * @bug Currently uses kmalloc (GFP_ATOMIC).
00045  */
00046 
00047 void *_stp_calloc(size_t len)
00048 {
00049         void *ptr = _stp_alloc(len);
00050         if (likely(ptr))
00051                 memset(ptr, 0, len);
00052         return ptr;
00053 }
00054 
00055 /** Allocates and clears memory outside a probe.
00056  * This is typically used in the module initialization to
00057  * allocate new maps, lists, etc.
00058  * @param len Number of bytes to allocate.
00059  * @return a valid pointer on success or NULL on failure.
00060  */
00061 
00062 void *_stp_valloc(size_t len)
00063 {
00064         void *ptr = vmalloc(len);
00065         if (likely(ptr))
00066                 memset(ptr, 0, len);
00067         else
00068                 _stp_error = ERR_NO_MEM;
00069         return ptr;
00070 }
00071 
00072 /** Frees memory allocated by _stp_alloc or _stp_calloc.
00073  * @param ptr pointer to memory to free
00074  */
00075 
00076 void _stp_free(void *ptr)
00077 {
00078         if (likely(ptr))
00079                 kfree(ptr);
00080 }
00081 
00082 /** Frees memory allocated by _stp_valloc.
00083  * @param ptr pointer to memory to free
00084  */
00085 
00086 void _stp_vfree(void *ptr)
00087 {
00088         if (likely(ptr))
00089                 vfree(ptr);
00090 }
00091 
00092 /** @} */
00093 #endif /* _ALLOC_C_ */