summaryrefslogtreecommitdiff
path: root/libc/malloc_init.cpp
blob: abe17a44dc96a0a43d45c75d3c498f72a98efbc9 (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
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*
 * Copyright (C) 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <private/bionic_config.h>
#include <private/bionic_globals.h>

#include <stdio.h>

#include <async_safe/log.h>

#if !defined(LIBC_STATIC)
extern "C" void* native_bridge_calloc(size_t, size_t);
extern "C" void native_bridge_free(void*);
extern "C" struct mallinfo native_bridge_mallinfo();
extern "C" void* native_bridge_malloc(size_t);
extern "C" size_t native_bridge_malloc_usable_size(const void*);
extern "C" void* native_bridge_memalign(size_t, size_t);
extern "C" int native_bridge_posix_memalign(void**, size_t, size_t);
extern "C" void* native_bridge_realloc(void*, size_t);
extern "C" int native_bridge_malloc_iterate(uintptr_t, size_t, void (*)(uintptr_t, size_t, void*), void*);
extern "C" void native_bridge_malloc_disable();
extern "C" void native_bridge_malloc_enable();
extern "C" int native_bridge_mallopt(int, int);
extern "C" void* native_bridge_aligned_alloc(size_t, size_t);

#if defined(HAVE_DEPRECATED_MALLOC_FUNCS)
extern "C" void* native_bridge_pvalloc(size_t);
extern "C" void* native_bridge_valloc(size_t);
#endif

extern "C" int native_bridge_malloc_info_helper(int options, int fd);

static int native_bridge_malloc_info(int options, FILE* fp) {
  // FILE objects cannot cross architecture boundary!
  // HACK: extract underlying file descriptor and use it instead.
  // TODO(b/146494184): at the moment malloc_info succeeds but writes nothing to memory streams!
  fflush(fp);
  int fd = fileno(fp);
  if (fd == -1) {
    return 0;
  }

  return native_bridge_malloc_info_helper(options, fd);
}

static void malloc_init_impl(libc_globals* globals) {
  static const MallocDispatch malloc_default_dispatch __attribute__((unused)) = {
    native_bridge_calloc,
    native_bridge_free,
    native_bridge_mallinfo,
    native_bridge_malloc,
    native_bridge_malloc_usable_size,
    native_bridge_memalign,
    native_bridge_posix_memalign,
#if defined(HAVE_DEPRECATED_MALLOC_FUNCS)
    native_bridge_pvalloc,
#endif
    native_bridge_realloc,
#if defined(HAVE_DEPRECATED_MALLOC_FUNCS)
    native_bridge_valloc,
#endif
    native_bridge_malloc_iterate,
    native_bridge_malloc_disable,
    native_bridge_malloc_enable,
    native_bridge_mallopt,
    native_bridge_aligned_alloc,
    native_bridge_malloc_info,
  };

  globals->malloc_dispatch_table = malloc_default_dispatch;
  atomic_store(&globals->default_dispatch_table, &malloc_default_dispatch);
  atomic_store(&globals->current_dispatch_table, &malloc_default_dispatch);
}

// Initializes memory allocation framework.
// This routine is called from __libc_init routines in libc_init_dynamic.cpp.
__LIBC_HIDDEN__ void __libc_init_malloc(libc_globals* globals) {
  malloc_init_impl(globals);
}
#endif  // !LIBC_STATIC