Move discard_const hack to memory.hso it can be used by Samba 3.
[kai/samba-autobuild/.git] / lib / util / util.h
index 861b6affe54d7355340da60e2f611123fcbe54f9..110286dbc865496b18e8a7b02890074774ce975b 100644 (file)
 #ifndef _SAMBA_UTIL_H_
 #define _SAMBA_UTIL_H_
 
+#if _SAMBA_BUILD_ == 4
+#include "lib/charset/charset.h"
+#endif
 #include "../lib/util/attr.h"
 
-#include "charset/charset.h"
-
 /* for TALLOC_CTX */
 #include <talloc.h>
 
@@ -44,33 +45,6 @@ extern const char *panic_action;
 #include "../lib/util/mutex.h"
 #include "../lib/util/byteorder.h"
 
-/**
-  this is a warning hack. The idea is to use this everywhere that we
-  get the "discarding const" warning from gcc. That doesn't actually
-  fix the problem of course, but it means that when we do get to
-  cleaning them up we can do it by searching the code for
-  discard_const.
-
-  It also means that other error types aren't as swamped by the noise
-  of hundreds of const warnings, so we are more likely to notice when
-  we get new errors.
-
-  Please only add more uses of this macro when you find it
-  _really_ hard to fix const warnings. Our aim is to eventually use
-  this function in only a very few places.
-
-  Also, please call this via the discard_const_p() macro interface, as that
-  makes the return type safe.
-*/
-#ifndef discard_const
-#define discard_const(ptr) ((void *)((uintptr_t)(ptr)))
-#endif
-
-/** Type-safe version of discard_const */
-#ifndef discard_const_p
-#define discard_const_p(type, ptr) ((type *)discard_const(ptr))
-#endif
-
 /**
  * assert macros 
  */
@@ -78,79 +52,11 @@ extern const char *panic_action;
        DEBUG(0,("PANIC: assert failed at %s(%d)\n", __FILE__, __LINE__)); \
        smb_panic("assert failed"); }} while (0)
 
-#ifndef SAFE_FREE /* Oh no this is also defined in tdb.h */
-/**
- * Free memory if the pointer and zero the pointer.
- *
- * @note You are explicitly allowed to pass NULL pointers -- they will
- * always be ignored.
- **/
-#define SAFE_FREE(x) do { if ((x) != NULL) {free(discard_const_p(void *, (x))); (x)=NULL;} } while(0)
-#endif
-
-/** 
- * Type-safe version of malloc. Allocated one copy of the 
- * specified data type.
- */
-#define malloc_p(type) (type *)malloc(sizeof(type))
-
-/**
- * Allocate an array of elements of one data type. Does type-checking.
- */
-#define malloc_array_p(type, count) (type *)realloc_array(NULL, sizeof(type), count)
-
-/** 
- * Resize an array of elements of one data type. Does type-checking.
- */
-#define realloc_p(p, type, count) (type *)realloc_array(p, sizeof(type), count)
-
 #if defined(VALGRIND)
 #define strlen(x) valgrind_strlen(x)
 #endif
 
-/** 
- * zero a structure 
- */
-#ifndef ZERO_STRUCT
-#define ZERO_STRUCT(x) memset((char *)&(x), 0, sizeof(x))
-#endif
-
-/** 
- * zero a structure given a pointer to the structure 
- */
-#ifndef ZERO_STRUCTP
-#define ZERO_STRUCTP(x) do { if ((x) != NULL) memset((char *)(x), 0, sizeof(*(x))); } while(0)
-#endif
-
-/** 
- * zero a structure given a pointer to the structure - no zero check 
- */
-#ifndef ZERO_STRUCTPN
-#define ZERO_STRUCTPN(x) memset((char *)(x), 0, sizeof(*(x)))
-#endif
-
-/* zero an array - note that sizeof(array) must work - ie. it must not be a
-   pointer */
-#ifndef ZERO_ARRAY
-#define ZERO_ARRAY(x) memset((char *)(x), 0, sizeof(x))
-#endif
-
-/**
- * work out how many elements there are in a static array 
- */
-#ifndef ARRAY_SIZE
-#define ARRAY_SIZE(a) (sizeof(a)/sizeof(a[0]))
-#endif
-
-/** 
- * pointer difference macro 
- */
-#ifndef PTR_DIFF
-#define PTR_DIFF(p1,p2) ((ptrdiff_t)(((const char *)(p1)) - (const char *)(p2)))
-#endif
-
-/* The following definitions come from lib/util/fault.c  */
-
+#include "../lib/util/memory.h"
 
 /**
  * Write backtrace to debug log
@@ -303,12 +209,12 @@ _PUBLIC_ char *safe_strcat(char *dest, const char *src, size_t maxlength);
 
 
 **/
-_PUBLIC_ size_t strhex_to_str(char *p, size_t len, const char *strhex);
+_PUBLIC_ size_t strhex_to_str(char *p, size_t p_len, const char *strhex, size_t strhex_len);
 
 /** 
  * Parse a hex string and return a data blob. 
  */
-_PUBLIC_ _PURE_ DATA_BLOB strhex_to_data_blob(const char *strhex) ;
+_PUBLIC_ _PURE_ DATA_BLOB strhex_to_data_blob(TALLOC_CTX *mem_ctx, const char *strhex) ;
 
 /**
  * Routine to print a buffer as HEX digits, into an allocated string.
@@ -452,7 +358,7 @@ _PUBLIC_ bool strequal(const char *s1, const char *s2);
   separator list. The separator list must contain characters less than
   or equal to 0x2f for this to work correctly on multi-byte strings
 */
-_PUBLIC_ const char **str_list_make(TALLOC_CTX *mem_ctx, const char *string, const char *sep);
+_PUBLIC_ char **str_list_make(TALLOC_CTX *mem_ctx, const char *string, const char *sep);
 
 /**
  * build a null terminated list of strings from an argv-like input string 
@@ -473,12 +379,12 @@ _PUBLIC_ char *str_list_join_shell(TALLOC_CTX *mem_ctx, const char **list, char
 /**
   return the number of elements in a string list
 */
-_PUBLIC_ size_t str_list_length(const char **list);
+_PUBLIC_ size_t str_list_length(const char * const *list);
 
 /**
   copy a string list
 */
-_PUBLIC_ const char **str_list_copy(TALLOC_CTX *mem_ctx, const char **list);
+_PUBLIC_ char **str_list_copy(TALLOC_CTX *mem_ctx, const char **list);
 
 /**
    Return true if all the elements of the list match exactly.
@@ -523,12 +429,12 @@ _PUBLIC_ char *afdgets(int fd, TALLOC_CTX *mem_ctx, size_t hint);
 /**
 load a file into memory from a fd.
 **/
-_PUBLIC_ char *fd_load(int fd, size_t *size, TALLOC_CTX *mem_ctx);
+_PUBLIC_ char *fd_load(int fd, size_t *size, size_t maxsize, TALLOC_CTX *mem_ctx);
 
 /**
 load a file into memory
 **/
-_PUBLIC_ char *file_load(const char *fname, size_t *size, TALLOC_CTX *mem_ctx);
+_PUBLIC_ char *file_load(const char *fname, size_t *size, size_t maxsize, TALLOC_CTX *mem_ctx);
 
 /**
 mmap (if possible) or read a file
@@ -539,14 +445,14 @@ _PUBLIC_ void *map_file(const char *fname, size_t size);
 load a file into memory and return an array of pointers to lines in the file
 must be freed with talloc_free(). 
 **/
-_PUBLIC_ char **file_lines_load(const char *fname, int *numlines, TALLOC_CTX *mem_ctx);
+_PUBLIC_ char **file_lines_load(const char *fname, int *numlines, size_t maxsize, TALLOC_CTX *mem_ctx);
 
 /**
 load a fd into memory and return an array of pointers to lines in the file
 must be freed with talloc_free(). If convert is true calls unix_to_dos on
 the list.
 **/
-_PUBLIC_ char **fd_lines_load(int fd, int *numlines, TALLOC_CTX *mem_ctx);
+_PUBLIC_ char **fd_lines_load(int fd, int *numlines, size_t maxsize, TALLOC_CTX *mem_ctx);
 
 /**
 take a list of lines and modify them to produce a list where \ continues
@@ -641,7 +547,7 @@ _PUBLIC_ bool same_net(struct in_addr ip1,struct in_addr ip2,struct in_addr mask
 /**
  Check if a process exists. Does this work on all unixes?
 **/
-_PUBLIC_ bool process_exists(pid_t pid);
+_PUBLIC_ bool process_exists_by_pid(pid_t pid);
 
 /**
  Simple routine to do POSIX file locking. Cruft in NFS and 64->32 bit mapping
@@ -701,7 +607,7 @@ _PUBLIC_ bool all_zero(const uint8_t *ptr, size_t size);
 /**
   realloc an array, checking for integer overflow in the array size
 */
-_PUBLIC_ void *realloc_array(void *ptr, size_t el_size, unsigned count);
+_PUBLIC_ void *realloc_array(void *ptr, size_t el_size, unsigned count, bool free_on_fail);
 
 /* The following definitions come from lib/util/fsusage.c  */