dbghelp: Speed up source string creation (by using rb trees).
[wine.git] / dlls / dbghelp / source.c
bloba5bb86f78f7b08566df981f4e196d8e4fa57b8fb
1 /*
2 * File source.c - source files management
4 * Copyright (C) 2004, Eric Pouech.
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 "config.h"
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <string.h>
25 #include <assert.h>
27 #include "dbghelp_private.h"
28 #include "wine/debug.h"
30 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp);
32 static struct module* rb_module;
33 struct source_rb
35 struct wine_rb_entry entry;
36 unsigned source;
39 static void *source_rb_alloc(size_t size)
41 return HeapAlloc(GetProcessHeap(), 0, size);
44 static void *source_rb_realloc(void *ptr, size_t size)
46 return HeapReAlloc(GetProcessHeap(), 0, ptr, size);
49 static void source_rb_free(void *ptr)
51 HeapFree(GetProcessHeap(), 0, ptr);
54 static int source_rb_compare(const void *key, const struct wine_rb_entry *entry)
56 const struct source_rb *t = WINE_RB_ENTRY_VALUE(entry, const struct source_rb, entry);
58 return strcmp((const char*)key, rb_module->sources + t->source);
61 const struct wine_rb_functions source_rb_functions =
63 source_rb_alloc,
64 source_rb_realloc,
65 source_rb_free,
66 source_rb_compare,
69 /******************************************************************
70 * source_find
72 * check whether a source file has already been stored
74 static unsigned source_find(const char* name)
76 struct wine_rb_entry* e;
78 e = wine_rb_get(&rb_module->sources_offsets_tree, name);
79 if (!e) return -1;
80 return WINE_RB_ENTRY_VALUE(e, struct source_rb, entry)->source;
83 /******************************************************************
84 * source_new
86 * checks if source exists. if not, add it
88 unsigned source_new(struct module* module, const char* base, const char* name)
90 unsigned ret;
91 const char* full;
92 char* tmp = NULL;
94 if (!name) return (unsigned)-1;
95 if (!base || *name == '/')
96 full = name;
97 else
99 unsigned bsz = strlen(base);
101 tmp = HeapAlloc(GetProcessHeap(), 0, bsz + 1 + strlen(name) + 1);
102 if (!tmp) return (unsigned)-1;
103 full = tmp;
104 strcpy(tmp, base);
105 if (tmp[bsz - 1] != '/') tmp[bsz++] = '/';
106 strcpy(&tmp[bsz], name);
108 rb_module = module;
109 if (!module->sources || (ret = source_find(full)) == (unsigned)-1)
111 char* new;
112 int len = strlen(full) + 1;
113 struct source_rb* rb;
115 if (module->sources_used + len + 1 > module->sources_alloc)
117 if (!module->sources)
119 module->sources_alloc = (module->sources_used + len + 1 + 255) & ~255;
120 new = HeapAlloc(GetProcessHeap(), 0, module->sources_alloc);
122 else
124 module->sources_alloc = max( module->sources_alloc * 2,
125 (module->sources_used + len + 1 + 255) & ~255 );
126 new = HeapReAlloc(GetProcessHeap(), 0, module->sources,
127 module->sources_alloc);
129 if (!new) goto done;
130 module->sources = new;
132 ret = module->sources_used;
133 memcpy(module->sources + module->sources_used, full, len);
134 module->sources_used += len;
135 module->sources[module->sources_used] = '\0';
136 if ((rb = pool_alloc(&module->pool, sizeof(*rb))))
138 rb->source = ret;
139 wine_rb_put(&module->sources_offsets_tree, full, &rb->entry);
142 done:
143 HeapFree(GetProcessHeap(), 0, tmp);
144 return ret;
147 /******************************************************************
148 * source_get
150 * returns a stored source file name
152 const char* source_get(const struct module* module, unsigned idx)
154 if (idx == -1) return "";
155 assert(module->sources);
156 return module->sources + idx;
159 /******************************************************************
160 * SymEnumSourceFiles (DBGHELP.@)
163 BOOL WINAPI SymEnumSourceFiles(HANDLE hProcess, ULONG64 ModBase, PCSTR Mask,
164 PSYM_ENUMSOURCEFILES_CALLBACK cbSrcFiles,
165 PVOID UserContext)
167 struct module_pair pair;
168 SOURCEFILE sf;
169 char* ptr;
171 if (!cbSrcFiles) return FALSE;
172 pair.pcs = process_find_by_handle(hProcess);
173 if (!pair.pcs) return FALSE;
175 if (ModBase)
177 pair.requested = module_find_by_addr(pair.pcs, ModBase, DMT_UNKNOWN);
178 if (!module_get_debug(&pair)) return FALSE;
180 else
182 if (Mask[0] == '!')
184 pair.requested = module_find_by_nameA(pair.pcs, Mask + 1);
185 if (!module_get_debug(&pair)) return FALSE;
187 else
189 FIXME("Unsupported yet (should get info from current context)\n");
190 return FALSE;
193 if (!pair.effective->sources) return FALSE;
194 for (ptr = pair.effective->sources; *ptr; ptr += strlen(ptr) + 1)
196 /* FIXME: not using Mask */
197 sf.ModBase = ModBase;
198 sf.FileName = ptr;
199 if (!cbSrcFiles(&sf, UserContext)) break;
202 return TRUE;
205 /******************************************************************
206 * SymGetSourceFileToken (DBGHELP.@)
209 BOOL WINAPI SymGetSourceFileToken(HANDLE hProcess, ULONG64 base,
210 PCSTR src, PVOID* token, DWORD* size)
212 FIXME("%p %s %s %p %p: stub!\n",
213 hProcess, wine_dbgstr_longlong(base), debugstr_a(src), token, size);
214 SetLastError(ERROR_NOT_SUPPORTED);
215 return FALSE;
218 /******************************************************************
219 * SymGetSourceFileTokenW (DBGHELP.@)
222 BOOL WINAPI SymGetSourceFileTokenW(HANDLE hProcess, ULONG64 base,
223 PCWSTR src, PVOID* token, DWORD* size)
225 FIXME("%p %s %s %p %p: stub!\n",
226 hProcess, wine_dbgstr_longlong(base), debugstr_w(src), token, size);
227 SetLastError(ERROR_NOT_SUPPORTED);
228 return FALSE;