summaryrefslogtreecommitdiff
path: root/server/util/memory.c
blob: a25aa787222414b46c088531e656040073b5b499 (plain)
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
#include "replace.h"
#include "talloc.h"

/*
 * sssd_mem_takeover
 * This function will take a non-talloc pointer and add it to a talloc
 * memory context. It will accept a destructor for the original pointer
 * so that when the parent memory context is freed, the non-talloc
 * pointer will also be freed properly.
 */
TALLOC_CTX *sssd_mem_takeover(TALLOC_CTX *mem_ctx,
                              void *ptr,
                              int (*destructor)(void **))
{
    TALLOC_CTX **handle;

    if (ptr == NULL) {
        return NULL;
    }

    handle = talloc_named_const(mem_ctx, sizeof(void *),
                                "sssd_mem_takeover destructor handle");
    if (handle == NULL) {
        return NULL;
    }

    *handle = ptr;
    talloc_set_destructor(handle, destructor);

    return handle;
}