~efargaspro/+junk/codeblocks-16.01-release

« back to all changes in this revision

Viewing changes to src/include/mozilla_chardet/xpcom/glue/nsMemory.h.org

  • Committer: damienlmoore at gmail
  • Date: 2016-02-02 02:43:22 UTC
  • Revision ID: damienlmoore@gmail.com-20160202024322-yql5qmtbwdyamdwd
Code::BlocksĀ 16.01

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
 
2
/* This Source Code Form is subject to the terms of the Mozilla Public
 
3
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 
4
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
5
 
 
6
#ifndef nsMemory_h__
 
7
#define nsMemory_h__
 
8
 
 
9
#include "nsXPCOM.h"
 
10
 
 
11
class nsIMemory;
 
12
 
 
13
#define NS_MEMORY_CONTRACTID "@mozilla.org/xpcom/memory-service;1"
 
14
#define NS_MEMORY_CID                                \
 
15
{ /* 30a04e40-38e7-11d4-8cf5-0060b0fc14a3 */         \
 
16
    0x30a04e40,                                      \
 
17
    0x38e7,                                          \
 
18
    0x11d4,                                          \
 
19
    {0x8c, 0xf5, 0x00, 0x60, 0xb0, 0xfc, 0x14, 0xa3} \
 
20
}
 
21
 
 
22
 
 
23
/**
 
24
 * Static helper routines to manage memory. These routines allow easy access
 
25
 * to xpcom's built-in (global) nsIMemory implementation, without needing
 
26
 * to go through the service manager to get it. However this requires clients
 
27
 * to link with the xpcom DLL. 
 
28
 *
 
29
 * This class is not threadsafe and is intented for use only on the main
 
30
 * thread.
 
31
 */
 
32
class nsMemory
 
33
{
 
34
public:
 
35
    static NS_HIDDEN_(void*) Alloc(size_t size)
 
36
        { return NS_Alloc(size); }
 
37
 
 
38
    static NS_HIDDEN_(void*) Realloc(void* ptr, size_t size)
 
39
        { return NS_Realloc(ptr, size); }
 
40
 
 
41
    static NS_HIDDEN_(void) Free(void* ptr)
 
42
        { NS_Free(ptr); }
 
43
 
 
44
    static NS_COM_GLUE nsresult   HeapMinimize(bool aImmediate);
 
45
    static NS_COM_GLUE void*      Clone(const void* ptr, size_t size);
 
46
    static NS_COM_GLUE nsIMemory* GetGlobalMemoryService();       // AddRefs
 
47
};
 
48
 
 
49
/** 
 
50
 * Macro to free all elements of an XPCOM array of a given size using
 
51
 * freeFunc, then frees the array itself using nsMemory::Free().  
 
52
 *
 
53
 * Note that this macro (and its wrappers) can be used to deallocate a
 
54
 * partially- or completely-built array while unwinding an error
 
55
 * condition inside the XPCOM routine that was going to return the
 
56
 * array.  For this to work on a partially-built array, your code
 
57
 * needs to be building the array from index 0 upwards, and simply
 
58
 * pass the number of elements that have already been built (and thus
 
59
 * need to be freed) as |size|.
 
60
 *
 
61
 * Thanks to <alecf@netscape.com> for suggesting this form, which
 
62
 * allows the macro to be used with NS_RELEASE / NS_RELEASE_IF in
 
63
 * addition to nsMemory::Free.
 
64
 * 
 
65
 * @param size      Number of elements in the array.  If not a constant, this 
 
66
 *                  should be a int32_t.  Note that this means this macro 
 
67
 *                  will not work if size >= 2^31.
 
68
 * @param array     The array to be freed.
 
69
 * @param freeFunc  The function or macro to be used to free it. 
 
70
 *                  For arrays of nsISupports (or any class derived
 
71
 *                  from it), NS_IF_RELEASE (or NS_RELEASE) should be
 
72
 *                  passed as freeFunc.  For most (all?) other pointer
 
73
 *                  types (including XPCOM strings and wstrings),
 
74
 *                  nsMemory::Free should be used, since the
 
75
 *                  shared-allocator (nsMemory) is what will have been
 
76
 *                  used to allocate the memory.  
 
77
 */
 
78
#define NS_FREE_XPCOM_POINTER_ARRAY(size, array, freeFunc)                    \
 
79
    PR_BEGIN_MACRO                                                            \
 
80
        int32_t iter_ = int32_t(size);                                        \
 
81
        while (--iter_ >= 0)                                                  \
 
82
            freeFunc((array)[iter_]);                                         \
 
83
        NS_Free((array));                                                     \
 
84
    PR_END_MACRO
 
85
 
 
86
// convenience macros for commonly used calls.  mmmmm.  syntactic sugar.
 
87
 
 
88
/** 
 
89
 * Macro to free arrays of non-refcounted objects allocated by the
 
90
 * shared allocator (nsMemory) such as strings and wstrings.  A
 
91
 * convenience wrapper around NS_FREE_XPCOM_POINTER_ARRAY.
 
92
 *
 
93
 * @param size      Number of elements in the array.  If not a constant, this 
 
94
 *                  should be a int32_t.  Note that this means this macro 
 
95
 *                  will not work if size >= 2^31.
 
96
 * @param array     The array to be freed.
 
97
 */
 
98
#define NS_FREE_XPCOM_ALLOCATED_POINTER_ARRAY(size, array)                    \
 
99
    NS_FREE_XPCOM_POINTER_ARRAY((size), (array), NS_Free)
 
100
 
 
101
/**
 
102
 * Macro to free an array of pointers to nsISupports (or classes
 
103
 * derived from it).  A convenience wrapper around
 
104
 * NS_FREE_XPCOM_POINTER_ARRAY.
 
105
 *
 
106
 * Note that if you know that none of your nsISupports pointers are
 
107
 * going to be 0, you can gain a bit of speed by calling
 
108
 * NS_FREE_XPCOM_POINTER_ARRAY directly and using NS_RELEASE as your
 
109
 * free function.
 
110
 *
 
111
 * @param size      Number of elements in the array.  If not a constant, this 
 
112
 *                  should be a int32_t.  Note that this means this macro 
 
113
 *                  will not work if size >= 2^31.
 
114
 * @param array     The array to be freed.
 
115
 */
 
116
#define NS_FREE_XPCOM_ISUPPORTS_POINTER_ARRAY(size, array)                    \
 
117
    NS_FREE_XPCOM_POINTER_ARRAY((size), (array), NS_IF_RELEASE)
 
118
 
 
119
/**
 
120
 * Helpful array length function for calculating the length of a
 
121
 * statically declared array.
 
122
 */
 
123
 
 
124
#define NS_ARRAY_LENGTH(array_) \
 
125
  (sizeof(array_)/sizeof(array_[0]))
 
126
 
 
127
/**
 
128
 * A macro, NS_ALIGNMENT_OF(t_) that determines the alignment
 
129
 * requirements of a type.
 
130
 */
 
131
namespace mozilla {
 
132
  template <class T>
 
133
  struct AlignmentTestStruct
 
134
  {
 
135
    char c;
 
136
    T t;
 
137
  };
 
138
}
 
139
 
 
140
#define NS_ALIGNMENT_OF(t_) \
 
141
  (sizeof(mozilla::AlignmentTestStruct<t_>) - sizeof(t_))
 
142
 
 
143
/**
 
144
 * An enumeration type used to represent a method of assignment.
 
145
 */
 
146
enum nsAssignmentType {
 
147
    NS_ASSIGNMENT_COPY,   // copy by value
 
148
    NS_ASSIGNMENT_DEPEND, // copy by reference
 
149
    NS_ASSIGNMENT_ADOPT   // copy by reference (take ownership of resource)
 
150
};
 
151
 
 
152
#endif // nsMemory_h__
 
153