File: main-override-dep.cpp

package info (click to toggle)
mimalloc 3.2.7%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 1,336 kB
  • sloc: ansic: 16,817; cpp: 489; makefile: 21
file content (60 lines) | stat: -rw-r--r-- 1,241 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// Issue #981: test overriding allocation in a DLL that is compiled independent of mimalloc.
// This is imported by the `mimalloc-test-override` project.
#include <string>
#include <iostream>
#include "main-override-dep.h"

std::string TestAllocInDll::GetString()
{
	char* test = new char[128];
	memset(test, 0, 128);
	const char* t = "test";
	memcpy(test, t, 4);
	std::string r = test;
  std::cout << "override-dep: GetString: " << r << "\n";
	delete[] test;
	return r;
}

#include <windows.h>

void TestAllocInDll::TestHeapAlloc()
{
	HANDLE theap = GetProcessHeap();
	int* p = (int*)HeapAlloc(theap, 0, sizeof(int));
	*p = 42;
	HeapFree(theap, 0, p);
}

class Static {
private:
  void* p;
public:
  Static() {
    printf("override-dep: static constructor\n");
    p = malloc(64);
    return;
  }
  ~Static() {
    free(p);
    printf("override-dep: static destructor\n");
    return;
  }
};

static Static s = Static();


#include <windows.h>

BOOL WINAPI DllMain(HINSTANCE module, DWORD reason, LPVOID reserved) {
  (void)(reserved);
  (void)(module);
  if (reason==DLL_PROCESS_ATTACH) {
    printf("override-dep: dll attach\n");
  }
  else if (reason==DLL_PROCESS_DETACH) {
    printf("override-dep: dll detach\n");
  }
  return TRUE;
}