]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - android-sdk/platform-bionic.git/blob - linker/linker_allocator.h
Merge "Reduce stack usage of tmpfile(3)."
[android-sdk/platform-bionic.git] / linker / linker_allocator.h
1 /*
2  * Copyright (C) 2014 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
17 #ifndef __LINKER_ALLOCATOR_H
18 #define __LINKER_ALLOCATOR_H
20 #include <stdlib.h>
21 #include <limits.h>
22 #include "private/bionic_macros.h"
24 struct LinkerAllocatorPage;
26 /*
27  * This class is a non-template version of the LinkerAllocator
28  * It keeps code inside .cpp file by keeping the interface
29  * template-free.
30  *
31  * Please use LinkerAllocator<type> where possible (everywhere).
32  */
33 class LinkerBlockAllocator {
34  public:
35   LinkerBlockAllocator();
37   void init(size_t block_size);
38   void* alloc();
39   void free(void* block);
40   void protect_page(void* block, int prot);
41   void protect_all(int prot);
43  private:
44   void create_new_page();
45   LinkerAllocatorPage* find_page(void* block);
47   size_t block_size_;
48   LinkerAllocatorPage* page_list_;
49   void* free_block_list_;
51   DISALLOW_COPY_AND_ASSIGN(LinkerBlockAllocator);
52 };
54 /*
55  * A simple allocator for the dynamic linker. An allocator allocates instances
56  * of a single fixed-size type. Allocations are backed by page-sized private
57  * anonymous mmaps.
58  */
59 template<typename T>
60 class LinkerAllocator {
61  public:
62   LinkerAllocator() : block_allocator_() {}
63   void init() { block_allocator_.init(sizeof(T)); }
64   T* alloc() { return reinterpret_cast<T*>(block_allocator_.alloc()); }
65   void free(T* t) { block_allocator_.free(t); }
66   void protect_page(T* t, int prot) { block_allocator_.protect_page(t, prot); }
67   void protect_all(int prot) { block_allocator_.protect_all(prot); }
68  private:
69   LinkerBlockAllocator block_allocator_;
70   DISALLOW_COPY_AND_ASSIGN(LinkerAllocator);
71 };
72 #endif // __LINKER_ALLOCATOR_H