Made WebRTC tests report JS errors better.
[chromium-blink-merge.git] / base / allocator / win_allocator.cc
blobee451f546101c008c0b081f4c880790c20027bd6
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 // This is a simple allocator based on the windows heap.
7 extern "C" {
9 HANDLE win_heap;
11 bool win_heap_init(bool use_lfh) {
12 win_heap = HeapCreate(0, 0, 0);
13 if (win_heap == NULL)
14 return false;
16 if (use_lfh) {
17 ULONG enable_lfh = 2;
18 HeapSetInformation(win_heap, HeapCompatibilityInformation,
19 &enable_lfh, sizeof(enable_lfh));
20 // NOTE: Setting LFH may fail. Vista already has it enabled.
21 // And under the debugger, it won't use LFH. So we
22 // ignore any errors.
25 return true;
28 void* win_heap_malloc(size_t size) {
29 return HeapAlloc(win_heap, 0, size);
32 void win_heap_free(void* size) {
33 HeapFree(win_heap, 0, size);
36 void* win_heap_realloc(void* ptr, size_t size) {
37 if (!ptr)
38 return win_heap_malloc(size);
39 if (!size) {
40 win_heap_free(ptr);
41 return NULL;
43 return HeapReAlloc(win_heap, 0, ptr, size);
46 size_t win_heap_msize(void* ptr) {
47 return HeapSize(win_heap, 0, ptr);
50 void* win_heap_memalign(size_t alignment, size_t size) {
51 // Reserve enough space to ensure we can align and set aligned_ptr[-1] to the
52 // original allocation for use with win_heap_memalign_free() later.
53 size_t allocation_size = size + (alignment - 1) + sizeof(void*);
55 // Check for overflow. Alignment and size are checked in allocator_shim.
56 DCHECK_LT(size, allocation_size);
57 DCHECK_LT(alignment, allocation_size);
59 // Since we're directly calling the allocator function, before OOM handling,
60 // we need to NULL check to ensure the allocation succeeded.
61 void* ptr = win_heap_malloc(allocation_size);
62 if (!ptr)
63 return ptr;
65 char* aligned_ptr = static_cast<char*>(ptr) + sizeof(void*);
66 aligned_ptr +=
67 alignment - reinterpret_cast<uintptr_t>(aligned_ptr) & (alignment - 1);
69 reinterpret_cast<void**>(aligned_ptr)[-1] = ptr;
70 return aligned_ptr;
73 void win_heap_memalign_free(void* ptr) {
74 if (ptr)
75 win_heap_free(static_cast<void**>(ptr)[-1]);
78 } // extern "C"