intl/icu/source/common/cmemory.c

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

michael@0 1 /*
michael@0 2 ******************************************************************************
michael@0 3 *
michael@0 4 * Copyright (C) 2002-2012, International Business Machines
michael@0 5 * Corporation and others. All Rights Reserved.
michael@0 6 *
michael@0 7 ******************************************************************************
michael@0 8 *
michael@0 9 * File cmemory.c ICU Heap allocation.
michael@0 10 * All ICU heap allocation, both for C and C++ new of ICU
michael@0 11 * class types, comes through these functions.
michael@0 12 *
michael@0 13 * If you have a need to replace ICU allocation, this is the
michael@0 14 * place to do it.
michael@0 15 *
michael@0 16 * Note that uprv_malloc(0) returns a non-NULL pointer, and
michael@0 17 * that a subsequent free of that pointer value is a NOP.
michael@0 18 *
michael@0 19 ******************************************************************************
michael@0 20 */
michael@0 21 #include "unicode/uclean.h"
michael@0 22 #include "cmemory.h"
michael@0 23 #include "putilimp.h"
michael@0 24 #include "uassert.h"
michael@0 25 #include <stdlib.h>
michael@0 26
michael@0 27 /* uprv_malloc(0) returns a pointer to this read-only data. */
michael@0 28 static const int32_t zeroMem[] = {0, 0, 0, 0, 0, 0};
michael@0 29
michael@0 30 /* Function Pointers for user-supplied heap functions */
michael@0 31 static const void *pContext;
michael@0 32 static UMemAllocFn *pAlloc;
michael@0 33 static UMemReallocFn *pRealloc;
michael@0 34 static UMemFreeFn *pFree;
michael@0 35
michael@0 36 /* Flag indicating whether any heap allocations have happened.
michael@0 37 * Used to prevent changing out the heap functions after allocations have been made */
michael@0 38 static UBool gHeapInUse;
michael@0 39
michael@0 40 #if U_DEBUG && defined(UPRV_MALLOC_COUNT)
michael@0 41 #include <stdio.h>
michael@0 42 static int n=0;
michael@0 43 static long b=0;
michael@0 44 #endif
michael@0 45
michael@0 46 #if U_DEBUG
michael@0 47
michael@0 48 static char gValidMemorySink = 0;
michael@0 49
michael@0 50 U_CAPI void uprv_checkValidMemory(const void *p, size_t n) {
michael@0 51 /*
michael@0 52 * Access the memory to ensure that it's all valid.
michael@0 53 * Load and save a computed value to try to ensure that the compiler
michael@0 54 * does not throw away the whole loop.
michael@0 55 * A thread analyzer might complain about un-mutexed access to gValidMemorySink
michael@0 56 * which is true but harmless because no one ever uses the value in gValidMemorySink.
michael@0 57 */
michael@0 58 const char *s = (const char *)p;
michael@0 59 char c = gValidMemorySink;
michael@0 60 size_t i;
michael@0 61 U_ASSERT(p != NULL);
michael@0 62 for(i = 0; i < n; ++i) {
michael@0 63 c ^= s[i];
michael@0 64 }
michael@0 65 gValidMemorySink = c;
michael@0 66 }
michael@0 67
michael@0 68 #endif /* U_DEBUG */
michael@0 69
michael@0 70 U_CAPI void * U_EXPORT2
michael@0 71 uprv_malloc(size_t s) {
michael@0 72 #if U_DEBUG && defined(UPRV_MALLOC_COUNT)
michael@0 73 #if 1
michael@0 74 putchar('>');
michael@0 75 fflush(stdout);
michael@0 76 #else
michael@0 77 fprintf(stderr,"MALLOC\t#%d\t%ul bytes\t%ul total\n", ++n,s,(b+=s)); fflush(stderr);
michael@0 78 #endif
michael@0 79 #endif
michael@0 80 if (s > 0) {
michael@0 81 gHeapInUse = TRUE;
michael@0 82 if (pAlloc) {
michael@0 83 return (*pAlloc)(pContext, s);
michael@0 84 } else {
michael@0 85 return uprv_default_malloc(s);
michael@0 86 }
michael@0 87 } else {
michael@0 88 return (void *)zeroMem;
michael@0 89 }
michael@0 90 }
michael@0 91
michael@0 92 U_CAPI void * U_EXPORT2
michael@0 93 uprv_realloc(void * buffer, size_t size) {
michael@0 94 #if U_DEBUG && defined(UPRV_MALLOC_COUNT)
michael@0 95 putchar('~');
michael@0 96 fflush(stdout);
michael@0 97 #endif
michael@0 98 if (buffer == zeroMem) {
michael@0 99 return uprv_malloc(size);
michael@0 100 } else if (size == 0) {
michael@0 101 if (pFree) {
michael@0 102 (*pFree)(pContext, buffer);
michael@0 103 } else {
michael@0 104 uprv_default_free(buffer);
michael@0 105 }
michael@0 106 return (void *)zeroMem;
michael@0 107 } else {
michael@0 108 gHeapInUse = TRUE;
michael@0 109 if (pRealloc) {
michael@0 110 return (*pRealloc)(pContext, buffer, size);
michael@0 111 } else {
michael@0 112 return uprv_default_realloc(buffer, size);
michael@0 113 }
michael@0 114 }
michael@0 115 }
michael@0 116
michael@0 117 U_CAPI void U_EXPORT2
michael@0 118 uprv_free(void *buffer) {
michael@0 119 #if U_DEBUG && defined(UPRV_MALLOC_COUNT)
michael@0 120 putchar('<');
michael@0 121 fflush(stdout);
michael@0 122 #endif
michael@0 123 if (buffer != zeroMem) {
michael@0 124 if (pFree) {
michael@0 125 (*pFree)(pContext, buffer);
michael@0 126 } else {
michael@0 127 uprv_default_free(buffer);
michael@0 128 }
michael@0 129 }
michael@0 130 }
michael@0 131
michael@0 132 U_CAPI void * U_EXPORT2
michael@0 133 uprv_calloc(size_t num, size_t size) {
michael@0 134 void *mem = NULL;
michael@0 135 size *= num;
michael@0 136 mem = uprv_malloc(size);
michael@0 137 if (mem) {
michael@0 138 uprv_memset(mem, 0, size);
michael@0 139 }
michael@0 140 return mem;
michael@0 141 }
michael@0 142
michael@0 143 U_CAPI void U_EXPORT2
michael@0 144 u_setMemoryFunctions(const void *context, UMemAllocFn *a, UMemReallocFn *r, UMemFreeFn *f, UErrorCode *status)
michael@0 145 {
michael@0 146 if (U_FAILURE(*status)) {
michael@0 147 return;
michael@0 148 }
michael@0 149 if (a==NULL || r==NULL || f==NULL) {
michael@0 150 *status = U_ILLEGAL_ARGUMENT_ERROR;
michael@0 151 return;
michael@0 152 }
michael@0 153 if (gHeapInUse) {
michael@0 154 *status = U_INVALID_STATE_ERROR;
michael@0 155 return;
michael@0 156 }
michael@0 157 pContext = context;
michael@0 158 pAlloc = a;
michael@0 159 pRealloc = r;
michael@0 160 pFree = f;
michael@0 161 }
michael@0 162
michael@0 163
michael@0 164 U_CFUNC UBool cmemory_cleanup(void) {
michael@0 165 pContext = NULL;
michael@0 166 pAlloc = NULL;
michael@0 167 pRealloc = NULL;
michael@0 168 pFree = NULL;
michael@0 169 gHeapInUse = FALSE;
michael@0 170 return TRUE;
michael@0 171 }
michael@0 172
michael@0 173
michael@0 174 /*
michael@0 175 * gHeapInUse
michael@0 176 * Return True if ICU has allocated any memory.
michael@0 177 * Used by u_SetMutexFunctions() and similar to verify that ICU has not
michael@0 178 * been used, that it is in a pristine initial state.
michael@0 179 */
michael@0 180 U_CFUNC UBool cmemory_inUse() {
michael@0 181 return gHeapInUse;
michael@0 182 }
michael@0 183

mercurial