10e44a688STrond Norbye/* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
20e44a688STrond Norbye//
30e44a688STrond Norbye// This small test program demonstrate a crash on Windows in cb_create_thread
40e44a688STrond Norbye// (Inside CreateThread)
50e44a688STrond Norbye//
60e44a688STrond Norbye#include "config.h"
70e44a688STrond Norbye#include <cassert>
80e44a688STrond Norbye
90e44a688STrond Norbye#include "daemon/alloc_hooks.h"
100e44a688STrond Norbye
110e44a688STrond Norbyesize_t alloc_size;
120e44a688STrond Norbye
130e44a688STrond Norbyeextern "C" {
140e44a688STrond Norbye    static void NewHook(const void* ptr, size_t) {
150e44a688STrond Norbye        if (ptr != NULL) {
160e44a688STrond Norbye            void* p = const_cast<void*>(ptr);
170e44a688STrond Norbye            alloc_size = mc_get_allocation_size(p);
180e44a688STrond Norbye        }
190e44a688STrond Norbye    }
200e44a688STrond Norbye
210e44a688STrond Norbye    static void DeleteHook(const void* ptr) {
220e44a688STrond Norbye        if (ptr != NULL) {
230e44a688STrond Norbye            void* p = const_cast<void*>(ptr);
240e44a688STrond Norbye            alloc_size = mc_get_allocation_size(p);
250e44a688STrond Norbye        }
260e44a688STrond Norbye    }
270e44a688STrond Norbye
280e44a688STrond Norbye    static void TestThread(void* arg) {
290e44a688STrond Norbye        alloc_size = 0;
300e44a688STrond Norbye
310e44a688STrond Norbye        char *p = new char[100];
320e44a688STrond Norbye        assert(alloc_size > 0);
330e44a688STrond Norbye        size_t allocated = alloc_size;
340e44a688STrond Norbye
350e44a688STrond Norbye        alloc_size = 0;
360e44a688STrond Norbye        delete []p;
370e44a688STrond Norbye
380e44a688STrond Norbye        assert(alloc_size == allocated);
390e44a688STrond Norbye    }
400e44a688STrond Norbye}
410e44a688STrond Norbye
420e44a688STrond Norbyeint main(void) {
430e44a688STrond Norbye   init_alloc_hooks();
440e44a688STrond Norbye
450e44a688STrond Norbye   mc_add_new_hook(NewHook);
460e44a688STrond Norbye   mc_add_delete_hook(DeleteHook);
470e44a688STrond Norbye
480e44a688STrond Norbye   cb_thread_t tid;
490e44a688STrond Norbye   assert(cb_create_thread(&tid, TestThread, 0, 0) == 0);
500e44a688STrond Norbye   assert(cb_join_thread(tid) == 0);
510e44a688STrond Norbye
520e44a688STrond Norbye   mc_remove_new_hook(NewHook);
530e44a688STrond Norbye   mc_remove_delete_hook(DeleteHook);
540e44a688STrond Norbye
550e44a688STrond Norbye   return 0;
560e44a688STrond Norbye}
57