Changeset 47092 in webkit for trunk/JavaScriptCore/wtf/FastMalloc.h
- Timestamp:
- Aug 11, 2009, 11:22:41 PM (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/JavaScriptCore/wtf/FastMalloc.h
r47010 r47092 23 23 24 24 #include "Platform.h" 25 #include "PossiblyNull.h" 25 26 #include <stdlib.h> 26 27 #include <new> … … 34 35 void* fastRealloc(void*, size_t); 35 36 36 // These functions return 0 if an allocation fails. 37 void* tryFastMalloc(size_t); 38 void* tryFastZeroedMalloc(size_t); 39 void* tryFastCalloc(size_t numElements, size_t elementSize); 40 void* tryFastRealloc(void*, size_t); 37 struct TryMallocReturnValue { 38 TryMallocReturnValue(void* data) 39 : m_data(data) 40 { 41 } 42 TryMallocReturnValue(const TryMallocReturnValue& source) 43 : m_data(source.m_data) 44 { 45 source.m_data = 0; 46 } 47 ~TryMallocReturnValue() { ASSERT(!m_data); } 48 template <typename T> bool getValue(T& data) WARN_UNUSED_RETURN; 49 template <typename T> operator PossiblyNull<T>() 50 { 51 T value; 52 getValue(value); 53 return PossiblyNull<T>(value); 54 } 55 private: 56 mutable void* m_data; 57 }; 58 59 template <typename T> bool TryMallocReturnValue::getValue(T& data) { 60 union u { void* data; T target; } res; 61 res.data = m_data; 62 data = res.target; 63 bool returnValue = !!m_data; 64 m_data = 0; 65 return returnValue; 66 } 67 68 TryMallocReturnValue tryFastMalloc(size_t n); 69 TryMallocReturnValue tryFastZeroedMalloc(size_t n); 70 TryMallocReturnValue tryFastCalloc(size_t n_elements, size_t element_size); 71 TryMallocReturnValue tryFastRealloc(void* p, size_t n); 41 72 42 73 void fastFree(void*);
Note:
See TracChangeset
for help on using the changeset viewer.