dbghelp: Return a Unicode path in path_find_symbol_file().
[wine.git] / dlls / wsdapi / memory.c
blob89a6d2dc47d2c7ea9318dcf3a61778c8538c7f6c
1 /*
2 * Web Services on Devices
4 * Copyright 2017 Owen Rudge for CodeWeavers
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include <stdarg.h>
23 #define COBJMACROS
25 #include "windef.h"
26 #include "winbase.h"
27 #include "wine/debug.h"
28 #include "wine/list.h"
29 #include "wsdapi.h"
31 WINE_DEFAULT_DEBUG_CHANNEL(wsdapi);
33 #define MEMORY_ALLOCATION_MAGIC 0xB10C5EED
35 #define ALIGNMENT (2 * sizeof(void*))
36 #define ROUND_TO_ALIGNMENT(size) (((size) + ALIGNMENT - 1) & ~(ALIGNMENT - 1))
37 #define MEMORY_ALLOCATION_SIZE ROUND_TO_ALIGNMENT(sizeof(struct memory_allocation))
39 struct memory_allocation
41 int magic;
42 struct list entry;
44 struct list children;
47 static struct memory_allocation *find_allocation(void *ptr)
49 struct memory_allocation *allocation;
51 if (ptr == NULL)
53 return NULL;
56 allocation = (struct memory_allocation *)((char *)ptr - MEMORY_ALLOCATION_SIZE);
58 if (allocation->magic != MEMORY_ALLOCATION_MAGIC)
60 return NULL;
63 return allocation;
66 static void free_allocation(struct memory_allocation *item)
68 struct memory_allocation *child, *cursor;
70 LIST_FOR_EACH_ENTRY_SAFE(child, cursor, &item->children, struct memory_allocation, entry)
72 free_allocation(child);
75 list_remove(&item->entry);
76 item->magic = 0;
77 HeapFree(GetProcessHeap(), 0, item);
80 void * WINAPI WSDAllocateLinkedMemory(void *pParent, SIZE_T cbSize)
82 struct memory_allocation *allocation, *parent;
83 void *ptr;
85 TRACE("(%p, %lu)\n", pParent, cbSize);
87 ptr = HeapAlloc(GetProcessHeap(), 0, MEMORY_ALLOCATION_SIZE + cbSize);
89 if (ptr == NULL)
91 return NULL;
94 allocation = ptr;
95 allocation->magic = MEMORY_ALLOCATION_MAGIC;
97 list_init(&allocation->children);
99 /* See if we have a parent */
100 parent = find_allocation(pParent);
102 if (parent != NULL)
104 list_add_tail(&parent->children, &allocation->entry);
106 else
108 list_init(&allocation->entry);
111 return (char *)ptr + MEMORY_ALLOCATION_SIZE;
114 void WINAPI WSDAttachLinkedMemory(void *pParent, void *pChild)
116 struct memory_allocation *parent, *child;
118 TRACE("(%p, %p)\n", pParent, pChild);
120 child = find_allocation(pChild);
121 parent = find_allocation(pParent);
123 TRACE("child: %p, parent: %p\n", child, parent);
125 if ((child == NULL) || (parent == NULL))
127 return;
130 list_remove(&child->entry);
131 list_add_tail(&parent->children, &child->entry);
134 void WINAPI WSDDetachLinkedMemory(void *pVoid)
136 struct memory_allocation *allocation;
138 TRACE("(%p)\n", pVoid);
140 allocation = find_allocation(pVoid);
142 if (allocation == NULL)
144 TRACE("Memory allocation not found\n");
145 return;
148 list_remove(&allocation->entry);
151 void WINAPI WSDFreeLinkedMemory(void *pVoid)
153 struct memory_allocation *allocation;
155 TRACE("(%p)\n", pVoid);
157 allocation = find_allocation(pVoid);
159 if (allocation == NULL)
161 TRACE("Memory allocation not found\n");
162 return;
165 free_allocation(allocation);