├── FreeBSD └── devel │ └── lockfree-malloc │ ├── Makefile │ ├── distinfo │ └── pkg-descr ├── LICENSE ├── Makefile ├── README ├── README.md ├── aux_.h ├── l-singleton.h ├── lite-hooks-wrap.h ├── lite-hooks.h ├── lite-malloc.cpp ├── lite-malloc.h └── stack.h /FreeBSD/devel/lockfree-malloc/Makefile: -------------------------------------------------------------------------------- 1 | # New ports collection makefile for: lockfree-malloc 2 | # Date created: 2012-08-01 3 | # Whom: Gvozdikov Veniamin 4 | # 5 | # $FreeBSD$ 6 | # 7 | 8 | PORTNAME= lockfree-malloc 9 | PORTVERSION= 0.0.${DATE} 10 | CATEGORIES= devel 11 | MASTER_SITES= http://fbsd.zlonet.ru/distfiles/ 12 | 13 | MAINTAINER= g.veniamin@googlemail.com 14 | COMMENT= The world is first Web-scale memory allocator 15 | 16 | LICENSE= LGPL3 17 | 18 | DATE= 20120802 19 | ONLY_FOR_ARCHS= amd64 20 | USE_LDCONFIG= yes 21 | USE_BZIP2= yes 22 | 23 | OPTIONS= DOCS "Install README." on 24 | 25 | PLIST_FILES= lib/liblite-malloc-shared.so \ 26 | lib/liblite-malloc-static.a 27 | 28 | .include 29 | 30 | .if defined(WITH_DOCS) 31 | PLIST_FILES+= share/doc/${PORTNAME}/README 32 | PLIST_DIRS+= share/doc/${PORTNAME} 33 | .endif 34 | 35 | do-install: 36 | .for i in shared.so static.a 37 | ${INSTALL_LIB} ${WRKSRC}/liblite-malloc-${i} ${PREFIX}/lib/liblite-malloc-${i} 38 | .endfor 39 | post-install: 40 | .if defined(WITH_DOCS) 41 | ${MKDIR} ${DOCSDIR} 42 | cd ${WRKSRC} && ${INSTALL_DATA} README ${DOCSDIR} 43 | .endif 44 | 45 | .include 46 | -------------------------------------------------------------------------------- /FreeBSD/devel/lockfree-malloc/distinfo: -------------------------------------------------------------------------------- 1 | SHA256 (lockfree-malloc-0.0.20120802.tar.bz2) = 5b4ba8683fd0d02f1415d785d7fa04a41b1e3def8a1b690f434877d3931b16ea 2 | SIZE (lockfree-malloc-0.0.20120802.tar.bz2) = 41121 3 | -------------------------------------------------------------------------------- /FreeBSD/devel/lockfree-malloc/pkg-descr: -------------------------------------------------------------------------------- 1 | Here are the advantages of our allocator: 2 | 3 | * It's thread-friendly. It supports a practically-unlimited number of 4 | concurrent threads, without locking or performance degradation. 5 | * It's efficient, especially in a multi-threaded environment. Compared to 6 | a stock libc allocator, we see a significant performance boost. 7 | * It does NOT fragment or leak memory, unlike a stock libc allocator. 8 | * It wastes less memory. For small objects (less than 8kb in size), the 9 | overhead is around 0 bytes. (!) 10 | * It is designed from the ground-up for 64-bit architectures. 11 | * It is elegant. The whole codebase is only around 800 lines of fairly 12 | clean C++. (!) 13 | * It fully stand-alone; it does not rely on pthreads or libc at runtime. 14 | 15 | WWW: https://github.com/Begun/lockfree-malloc 16 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU LESSER GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | 9 | This version of the GNU Lesser General Public License incorporates 10 | the terms and conditions of version 3 of the GNU General Public 11 | License, supplemented by the additional permissions listed below. 12 | 13 | 0. Additional Definitions. 14 | 15 | As used herein, "this License" refers to version 3 of the GNU Lesser 16 | General Public License, and the "GNU GPL" refers to version 3 of the GNU 17 | General Public License. 18 | 19 | "The Library" refers to a covered work governed by this License, 20 | other than an Application or a Combined Work as defined below. 21 | 22 | An "Application" is any work that makes use of an interface provided 23 | by the Library, but which is not otherwise based on the Library. 24 | Defining a subclass of a class defined by the Library is deemed a mode 25 | of using an interface provided by the Library. 26 | 27 | A "Combined Work" is a work produced by combining or linking an 28 | Application with the Library. The particular version of the Library 29 | with which the Combined Work was made is also called the "Linked 30 | Version". 31 | 32 | The "Minimal Corresponding Source" for a Combined Work means the 33 | Corresponding Source for the Combined Work, excluding any source code 34 | for portions of the Combined Work that, considered in isolation, are 35 | based on the Application, and not on the Linked Version. 36 | 37 | The "Corresponding Application Code" for a Combined Work means the 38 | object code and/or source code for the Application, including any data 39 | and utility programs needed for reproducing the Combined Work from the 40 | Application, but excluding the System Libraries of the Combined Work. 41 | 42 | 1. Exception to Section 3 of the GNU GPL. 43 | 44 | You may convey a covered work under sections 3 and 4 of this License 45 | without being bound by section 3 of the GNU GPL. 46 | 47 | 2. Conveying Modified Versions. 48 | 49 | If you modify a copy of the Library, and, in your modifications, a 50 | facility refers to a function or data to be supplied by an Application 51 | that uses the facility (other than as an argument passed when the 52 | facility is invoked), then you may convey a copy of the modified 53 | version: 54 | 55 | a) under this License, provided that you make a good faith effort to 56 | ensure that, in the event an Application does not supply the 57 | function or data, the facility still operates, and performs 58 | whatever part of its purpose remains meaningful, or 59 | 60 | b) under the GNU GPL, with none of the additional permissions of 61 | this License applicable to that copy. 62 | 63 | 3. Object Code Incorporating Material from Library Header Files. 64 | 65 | The object code form of an Application may incorporate material from 66 | a header file that is part of the Library. You may convey such object 67 | code under terms of your choice, provided that, if the incorporated 68 | material is not limited to numerical parameters, data structure 69 | layouts and accessors, or small macros, inline functions and templates 70 | (ten or fewer lines in length), you do both of the following: 71 | 72 | a) Give prominent notice with each copy of the object code that the 73 | Library is used in it and that the Library and its use are 74 | covered by this License. 75 | 76 | b) Accompany the object code with a copy of the GNU GPL and this license 77 | document. 78 | 79 | 4. Combined Works. 80 | 81 | You may convey a Combined Work under terms of your choice that, 82 | taken together, effectively do not restrict modification of the 83 | portions of the Library contained in the Combined Work and reverse 84 | engineering for debugging such modifications, if you also do each of 85 | the following: 86 | 87 | a) Give prominent notice with each copy of the Combined Work that 88 | the Library is used in it and that the Library and its use are 89 | covered by this License. 90 | 91 | b) Accompany the Combined Work with a copy of the GNU GPL and this license 92 | document. 93 | 94 | c) For a Combined Work that displays copyright notices during 95 | execution, include the copyright notice for the Library among 96 | these notices, as well as a reference directing the user to the 97 | copies of the GNU GPL and this license document. 98 | 99 | d) Do one of the following: 100 | 101 | 0) Convey the Minimal Corresponding Source under the terms of this 102 | License, and the Corresponding Application Code in a form 103 | suitable for, and under terms that permit, the user to 104 | recombine or relink the Application with a modified version of 105 | the Linked Version to produce a modified Combined Work, in the 106 | manner specified by section 6 of the GNU GPL for conveying 107 | Corresponding Source. 108 | 109 | 1) Use a suitable shared library mechanism for linking with the 110 | Library. A suitable mechanism is one that (a) uses at run time 111 | a copy of the Library already present on the user's computer 112 | system, and (b) will operate properly with a modified version 113 | of the Library that is interface-compatible with the Linked 114 | Version. 115 | 116 | e) Provide Installation Information, but only if you would otherwise 117 | be required to provide such information under section 6 of the 118 | GNU GPL, and only to the extent that such information is 119 | necessary to install and execute a modified version of the 120 | Combined Work produced by recombining or relinking the 121 | Application with a modified version of the Linked Version. (If 122 | you use option 4d0, the Installation Information must accompany 123 | the Minimal Corresponding Source and Corresponding Application 124 | Code. If you use option 4d1, you must provide the Installation 125 | Information in the manner specified by section 6 of the GNU GPL 126 | for conveying Corresponding Source.) 127 | 128 | 5. Combined Libraries. 129 | 130 | You may place library facilities that are a work based on the 131 | Library side by side in a single library together with other library 132 | facilities that are not Applications and are not covered by this 133 | License, and convey such a combined library under terms of your 134 | choice, if you do both of the following: 135 | 136 | a) Accompany the combined library with a copy of the same work based 137 | on the Library, uncombined with any other library facilities, 138 | conveyed under the terms of this License. 139 | 140 | b) Give prominent notice with the combined library that part of it 141 | is a work based on the Library, and explaining where to find the 142 | accompanying uncombined form of the same work. 143 | 144 | 6. Revised Versions of the GNU Lesser General Public License. 145 | 146 | The Free Software Foundation may publish revised and/or new versions 147 | of the GNU Lesser General Public License from time to time. Such new 148 | versions will be similar in spirit to the present version, but may 149 | differ in detail to address new problems or concerns. 150 | 151 | Each version is given a distinguishing version number. If the 152 | Library as you received it specifies that a certain numbered version 153 | of the GNU Lesser General Public License "or any later version" 154 | applies to it, you have the option of following the terms and 155 | conditions either of that published version or of any later version 156 | published by the Free Software Foundation. If the Library as you 157 | received it does not specify a version number of the GNU Lesser 158 | General Public License, you may choose any version of the GNU Lesser 159 | General Public License ever published by the Free Software Foundation. 160 | 161 | If the Library as you received it specifies that a proxy can decide 162 | whether future versions of the GNU Lesser General Public License shall 163 | apply, that proxy's public statement of acceptance of any version is 164 | permanent authorization for you to choose that version for the 165 | Library. 166 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | 2 | CXX = g++ 3 | CFLAGS = -std=c++11 -O3 -finline-functions -Wno-inline -Wall -pthread 4 | LFLAGS = 5 | 6 | IDEPS = aux_.h lite-hooks-wrap.h lite-malloc.h stack.h l-singleton.h 7 | 8 | STATIC_LIB = liblite-malloc-static.a 9 | SHARED_LIB = liblite-malloc-shared.so 10 | 11 | all: $(STATIC_LIB) $(SHARED_LIB) 12 | 13 | lite-malloc.o: $(IDEPS) 14 | $(CXX) $(CFLAGS) lite-malloc.cpp -fPIC -c -o lite-malloc.o 15 | 16 | $(STATIC_LIB): lite-malloc.o 17 | -rm -f $(STATIC_LIB) 18 | ar rc $(STATIC_LIB) lite-malloc.o 19 | 20 | $(SHARED_LIB): lite-malloc.o 21 | objcopy --redefine-sym __wrap_malloc=malloc \ 22 | --redefine-sym __wrap_free=free \ 23 | --redefine-sym __wrap_calloc=calloc \ 24 | --redefine-sym __wrap_realloc=realloc \ 25 | --redefine-sym __wrap_memalign=memalign \ 26 | --redefine-sym __wrap_posix_memalign=posix_memalign \ 27 | --redefine-sym __wrap_valloc=valloc \ 28 | --redefine-sym __wrap_aligned_alloc=aligned_alloc \ 29 | lite-malloc.o lite-malloc-shared.o 30 | $(CXX) $(LFLAGS) lite-malloc-shared.o -shared -o $(SHARED_LIB) 31 | 32 | clean: 33 | -rm -f lite-malloc.o lite-malloc-shared.o $(STATIC_LIB) $(SHARED_LIB) 34 | 35 | 36 | -------------------------------------------------------------------------------- /README: -------------------------------------------------------------------------------- 1 | 2 | 3 | ABOUT 4 | ------------------------------------------------------------------- 5 | 6 | This is a drop-in replacement for malloc/free that is designed from 7 | the ground up to be used in scalable server software. 8 | 9 | Why another memory allocator? 10 | 11 | 12 | Here are the advantages of our allocator: 13 | 14 | * It's thread-friendly. It supports a practically-unlimited number of 15 | concurrent threads, without locking or performance degradation. 16 | 17 | * It's efficient, especially in a multi-threaded environment. 18 | Compared to a stock libc allocator, we see a significant 19 | performance boost. 20 | 21 | * It does NOT fragment or leak memory, unlike a stock libc allocator. 22 | 23 | * It wastes less memory. For small objects (less than 8kb in size), 24 | the overhead is around 0 bytes. (!) 25 | 26 | * It is designed from the ground-up for 64-bit architectures. 27 | 28 | * It is elegant. The whole codebase is only around 800 lines of 29 | fairly clean C++. (!) 30 | 31 | * It fully stand-alone; it does not rely on pthreads or libc at runtime. 32 | 33 | 34 | The disadvantages: 35 | 36 | * Works only with a x86_64 CPU. 37 | 38 | * It does not try to economise on virtual memory mapping space. 39 | 40 | * Memory used for small objects (those less than 128kb in size) is 41 | cached within the allocator and is never released back to the OS. 42 | In practice, this means that your app will stabilize at some peak 43 | usage of memory and will, from that moment on, rarely ask the OS 44 | for memory allocation or deallocation. 45 | 46 | NOTE: there is only limited support for the functions memalign, posix_memalign and valloc. 47 | 48 | USAGE 49 | ------------------------------------------------------------------- 50 | 51 | Build requirements: 52 | 53 | For building the library, you will need: 54 | 55 | * g++ or clang with C++11 support. 56 | 57 | 58 | Usage requirements: 59 | 60 | * An x86_64 CPU. 61 | 62 | * An OS with sane mmap/munmap support. 63 | We have production-tested only on Linux. (But it should work with FreeBSD too.) 64 | 65 | 66 | Compiling: 67 | 68 | Edit Makefile to set your build options; type 'make' to build a static 69 | and a shared version of the library. 70 | 71 | Note: we recommend integrating the allocator into your own 72 | project(s). The enclosed Makefile is intended only as a demonstration, 73 | not a complete build solution. 74 | 75 | 76 | Using the library: 77 | 78 | There are five options: 79 | 80 | 1. Compile into a statically-linked application. 81 | For this, you must: 82 | * Compile your application's object files as normal. 83 | * Compile 'lite-malloc.cpp', or use the 84 | 'liblite-malloc-static.a' generated with the enclosed Makefile. 85 | * Link your app with the '-static' flag, and these linker flags 86 | for wrapping: 87 | 88 | -Wl,--wrap,malloc -Wl,--wrap,free -Wl,--wrap,calloc 89 | -Wl,--wrap,realloc -Wl,--wrap,memalign -Wl,--wrap,valloc 90 | -Wl,--wrap,posix_memalign 91 | 92 | 2. Link with a shared library. Linking with 93 | 'liblite-malloc-shared.so' should work. 94 | 95 | 3. Via standard glibc malloc hooks. 96 | To enable, just include 'lite-hooks.h' as the first line of your program. 97 | 98 | 4. Via the magic LD_PRELOAD environment variable: 99 | 100 | $ LD_PRELOAD=/liblite-malloc-shared.so 101 | 102 | 5. Compile a malloc hook static library into your dynamically-linked application. 103 | Follow the instructions in option 1, except omit the '-static' flag, and add these linker flags: 104 | 105 | -Wl,--defsym=malloc=__wrap_malloc -Wl,--defsym=free=__wrap_free -Wl,--defsym=calloc=__wrap_calloc 106 | -Wl,--defsym=realloc=__wrap_realloc -Wl,--defsym=memalign=__wrap_memalign -Wl,--defsym=valloc=__wrap_valloc 107 | -Wl,--defsym=posix_memalign=__wrap_posix_memalign 108 | 109 | 110 | IMPORTANT NOTES: 111 | 112 | Linux has an OS-imposed limit on the number of virtual memory mappings. 113 | If you want to allocate large amounts of memory, you may need to adjust this 114 | limit via /proc/sys/vm/max_map_count. 115 | 116 | 117 | Configuration: 118 | 119 | The allocator can be tuned with compile-time defines. Here are the supported options: 120 | 121 | LITE_MALLOC_SUPERBLOCK_CACHE_SIZE 122 | 123 | (default is 32) 124 | 125 | Mmap blocks of size LITE_MALLOC_SUPERBLOCK_CACHE_SIZE * 4096 bytes and less will be cached. 126 | (With a separate cache per size / 4096 bytes.) 127 | 128 | LITE_MALLOC_MINIBLOCK_CACHE_SIZE 129 | 130 | (default is 1024) 131 | 132 | Allocations of size LITE_MALLOC_MINIBLOCK_CACHE_SIZE * 8 bytes and less will be cached. 133 | (With a separate cache per size / 8 bytes.) 134 | 135 | LITE_MALLOC_ENGINES_COUNT 136 | 137 | (default is 32) 138 | 139 | This many independent allocator engines will be used. 140 | 141 | LITE_MALLOC_USE_PERTHREAD_ENGINES 142 | 143 | (default is 1) 144 | 145 | If 1, pin a specific allocator engine to each thread. If 0, choose an allocator engine 146 | round-robin for each allocation. 147 | (Use 1 unless you have threads with highly unbalanced allocation workloads and are seeing 148 | dramatic overhead.) 149 | 150 | 151 | CREDITS 152 | ------------------------------------------------------------------- 153 | 154 | (c) 2011, ZAO "Begun" 155 | 156 | https://github.com/Begun/lockfree-malloc 157 | 158 | This library is licensed under the GNU LGPL. Please see the file LICENSE. 159 | 160 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | ## About ## 4 | 5 | This is a drop-in replacement for malloc/free that is designed from 6 | the ground up to be used in scalable server software. 7 | 8 | Why another memory allocator? 9 | 10 | 11 | Here are the advantages of our allocator: 12 | 13 | * It's thread-friendly. It supports a practically-unlimited number of concurrent threads, without locking or performance degradation. 14 | 15 | * It's efficient, especially in a multi-threaded environment. Compared to a stock libc allocator, we see a significant performance boost. 16 | 17 | * It does NOT fragment or leak memory, unlike a stock libc allocator. 18 | 19 | * It wastes less memory. For small objects (less than 8kb in size), the overhead is around 0 bytes. (!) 20 | 21 | * It is designed from the ground-up for 64-bit architectures. 22 | 23 | * It is elegant. The whole codebase is only around 700 lines of fairly clean C++. (!) 24 | 25 | * It fully stand-alone; it does not rely on pthreads or libc at runtime. 26 | 27 | 28 | The disadvantages: 29 | 30 | * Works only with a x86_64 CPU. 31 | 32 | * It does not try to economise on virtual memory mapping space. 33 | 34 | * Memory used for small objects (those less than 128kb in size) is cached within the allocator and is never released back to the OS. In practice, this means that your app will stabilize at some peak usage of memory and will, from that moment on, rarely ask the OS for memory allocation or deallocation. 35 | 36 | _Note_: there is only limited support for the functions memalign, posix_memalign and valloc. 37 | 38 | ## Usage ## 39 | 40 | Build requirements: 41 | 42 | For building the library, you will need: 43 | 44 | * g++ or clang with C++11 support. 45 | 46 | 47 | Usage requirements: 48 | 49 | * An x86_64 CPU. 50 | * An OS with sane mmap/munmap support. We have production-tested only on Linux. (But it should work with FreeBSD too.) 51 | 52 | 53 | ### Compiling ### 54 | 55 | Edit Makefile to set your build options; type `make` to build a static and a shared version of the library. 56 | 57 | _Note_: we recommend integrating the allocator into your own project(s). The enclosed Makefile is intended only as a demonstration, not a complete build solution. 58 | 59 | ### Using the library ### 60 | 61 | There are five options: 62 | 63 | 1. Compile into a statically-linked application. For this, you must: 64 | * Compile your application's object files as normal. 65 | * Compile `lite-malloc.cpp`, or use the `liblite-malloc-static.a` generated with the enclosed Makefile. 66 | * Link your app with the `-static` flag, and these linker flags for wrapping: 67 | ``` 68 | -Wl,--wrap,malloc -Wl,--wrap,free -Wl,--wrap,calloc -Wl,--wrap,realloc 69 | -Wl,--wrap,memalign -Wl,--wrap,valloc -Wl,--wrap,posix_memalign 70 | ``` 71 | 72 | 2. Link with a shared library. Linking with `liblite-malloc-shared.so` should work. 73 | 74 | 3. Via standard glibc malloc hooks. (Deprecated.) To enable, just include `lite-hooks.h` as the first line of your program. 75 | 76 | 4. Via the magic `LD_PRELOAD` environment variable: 77 | ``` 78 | $ LD_PRELOAD=/liblite-malloc-shared.so 79 | ``` 80 | 81 | 5. Compile a malloc hook static library into your dynamically-linked application. Follow the instructions in option 1, except omit the `-static` flag, and add these linker flags: 82 | ``` 83 | -Wl,--defsym=malloc=__wrap_malloc -Wl,--defsym=free=__wrap_free -Wl,--defsym=calloc=__wrap_calloc 84 | -Wl,--defsym=realloc=__wrap_realloc -Wl,--defsym=memalign=__wrap_memalign 85 | -Wl,--defsym=valloc=__wrap_valloc -Wl,--defsym=posix_memalign=__wrap_posix_memalign 86 | ``` 87 | 88 | *Important notes*: 89 | 90 | Linux has an OS-imposed limit on the number of virtual memory mappings. 91 | If you want to allocate large amounts of memory, you may need to adjust this 92 | limit via `/proc/sys/vm/max_map_count`. 93 | 94 | 95 | ### Configuration ### 96 | 97 | The allocator can be tuned with compile-time defines. Here are the supported options: 98 | 99 | ###### `LITE_MALLOC_SUPERBLOCK_CACHE_SIZE` 100 | 101 | (default is 32) 102 | 103 | Mmap blocks of size `LITE_MALLOC_SUPERBLOCK_CACHE_SIZE` * 4096 bytes and less will be cached. (With a separate cache per size / 4096 bytes.) 104 | 105 | ###### `LITE_MALLOC_MINIBLOCK_CACHE_SIZE` 106 | 107 | (default is 1024) 108 | 109 | Allocations of size `LITE_MALLOC_MINIBLOCK_CACHE_SIZE` * 8 bytes and less will be cached. (With a separate cache per size / 8 bytes.) 110 | 111 | ###### `LITE_MALLOC_ENGINES_COUNT` 112 | 113 | (default is 32) 114 | 115 | This many independent allocator engines will be used. 116 | 117 | ###### `LITE_MALLOC_USE_PERTHREAD_ENGINES` 118 | 119 | (default is 1) 120 | 121 | If 1, pin a specific allocator engine to each thread. If 0, choose an allocator engine round-robin for each allocation. 122 | 123 | (Use 1 unless you have threads with highly unbalanced allocation workloads and are seeing dramatic overhead.) 124 | 125 | 126 | ## Credits ## 127 | 128 | https://github.com/Begun/lockfree-malloc 129 | 130 | This library is licensed under the GNU LGPL. Please see the file LICENSE. 131 | 132 | -------------------------------------------------------------------------------- /aux_.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2011 ZAO "Begun". 2 | * 3 | * This library is free software; you can redistribute it and/or modify it under 4 | * the terms of the GNU Lesser General Public License as published by the Free 5 | * Software Foundation; either version 3 of the License, or (at your option) 6 | * any later version. 7 | * This library is distributed in the hope that it will be useful, but WITHOUT 8 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 9 | * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 10 | * details. 11 | * You should have received a copy of the GNU Lesser General Public License 12 | * along with this library. If not, see . 13 | */ 14 | 15 | // $Id$ 16 | 17 | #ifndef LOCKFREE_AUX_H 18 | #define LOCKFREE_AUX_H 1 19 | 20 | #include 21 | #include 22 | 23 | namespace lockfree 24 | { 25 | 26 | static size_t const ptr_size = sizeof (void *); 27 | 28 | template 29 | struct Linked 30 | { 31 | Linked () : next (NULL) {} 32 | T *next; 33 | }; 34 | 35 | struct Link : public Linked {}; 36 | 37 | 38 | //we have page size hardcoded; it is not quite correct, but still eases things 39 | //TODO: would it be any slower with get_page_size ()? 40 | size_t const base_page = 4 * 1024; 41 | 42 | } //namespace lockfree 43 | 44 | inline 45 | size_t 46 | align_up (size_t v, size_t alignment) 47 | { 48 | return (v + alignment - 1) / alignment * alignment; 49 | } 50 | 51 | namespace lf = lockfree; 52 | 53 | #endif //LOCKFREE_AUX_H 54 | -------------------------------------------------------------------------------- /l-singleton.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2011 ZAO "Begun". 2 | * 3 | * This library is free software; you can redistribute it and/or modify it under 4 | * the terms of the GNU Lesser General Public License as published by the Free 5 | * Software Foundation; either version 3 of the License, or (at your option) 6 | * any later version. 7 | * This library is distributed in the hope that it will be useful, but WITHOUT 8 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 9 | * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 10 | * details. 11 | * You should have received a copy of the GNU Lesser General Public License 12 | * along with this library. If not, see . 13 | */ 14 | 15 | #ifndef __LOCKFREE_SINGLETON_H 16 | #define __LOCKFREE_SINGLETON_H 1 17 | 18 | #include 19 | #include 20 | 21 | namespace lockfree 22 | { 23 | 24 | struct Once 25 | { 26 | constexpr Once() : l(2) {} 27 | 28 | std::atomic l; 29 | 30 | bool 31 | lock_once () 32 | { 33 | int c = l.load(std::memory_order_relaxed); 34 | 35 | for ( ; ; ) 36 | { 37 | if (c == 0) 38 | return false; 39 | 40 | if (c == 2) { 41 | if (l.compare_exchange_weak(c, 1, std::memory_order_release, std::memory_order_acquire)) { 42 | return true; 43 | } 44 | } else { 45 | c = l.load(std::memory_order_acquire); 46 | } 47 | } 48 | } 49 | 50 | void 51 | unlock () 52 | { 53 | l.store(0, std::memory_order_release); 54 | } 55 | }; 56 | 57 | template 58 | struct Raw_ 59 | { 60 | char data [sizeof (T)]; 61 | } __attribute__ ((aligned (16))); 62 | 63 | template 64 | struct Holder 65 | { 66 | static Raw_ value; 67 | static Once once; 68 | }; 69 | 70 | template Raw_ Holder ::value = {{}}; //TODO 71 | template Once Holder ::once; 72 | 73 | template 74 | T & 75 | singleton () 76 | { 77 | typedef Holder H; 78 | if (H::once.lock_once ()) 79 | { 80 | new (&H::value) T (); 81 | H::once.unlock (); 82 | } 83 | 84 | return (T &) H::value; 85 | } 86 | 87 | } //namespace lockfree 88 | 89 | #endif //__LOCKFREE_SINGLETON_H 90 | -------------------------------------------------------------------------------- /lite-hooks-wrap.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2011 ZAO "Begun". 2 | * 3 | * This library is free software; you can redistribute it and/or modify it under 4 | * the terms of the GNU Lesser General Public License as published by the Free 5 | * Software Foundation; either version 3 of the License, or (at your option) 6 | * any later version. 7 | * This library is distributed in the hope that it will be useful, but WITHOUT 8 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 9 | * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 10 | * details. 11 | * You should have received a copy of the GNU Lesser General Public License 12 | * along with this library. If not, see . 13 | */ 14 | 15 | #ifndef __LOCKFREE_LITE_HOOKS_WRAP_H 16 | #define __LOCKFREE_LITE_HOOKS_WRAP_H 1 17 | 18 | #include "lite-malloc.h" 19 | #ifndef __FreeBSD__ 20 | #include 21 | #endif 22 | 23 | #include 24 | 25 | #include 26 | 27 | extern "C" { 28 | 29 | void * __wrap_malloc (size_t size, void const *) __attribute__ ((malloc)); 30 | 31 | void * __wrap_malloc (size_t size, void const *) 32 | { 33 | return lockfree::singleton ().do_malloc (size); 34 | } 35 | 36 | void __wrap_free (void *p, void const *) 37 | { 38 | lockfree::singleton ().do_free (p); 39 | } 40 | 41 | void * __wrap_realloc (void *p, size_t size, void const *) 42 | { 43 | return lockfree::singleton ().do_realloc (p, size); 44 | } 45 | 46 | void * __wrap_calloc (size_t n, size_t size) 47 | { 48 | return lockfree::singleton ().do_calloc (n, size); 49 | } 50 | 51 | void * __wrap_memalign (size_t align, size_t size) 52 | { 53 | return lockfree::singleton ().do_memalign (align, size); 54 | } 55 | 56 | void * __wrap_aligned_alloc(size_t align, size_t size) 57 | { 58 | return __wrap_memalign (align, size); 59 | } 60 | 61 | int __wrap_posix_memalign(void **memptr, size_t align, size_t size) { 62 | *memptr = __wrap_memalign (align, size); 63 | return 0; 64 | } 65 | 66 | void * __wrap_valloc(size_t size) 67 | { 68 | return __wrap_memalign (lf::base_page, size); 69 | } 70 | 71 | 72 | 73 | } 74 | 75 | 76 | #endif //__LOCKFREE_LITE_HOOKS_WRAP_H 77 | -------------------------------------------------------------------------------- /lite-hooks.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2011 ZAO "Begun". 2 | * 3 | * This library is free software; you can redistribute it and/or modify it under 4 | * the terms of the GNU Lesser General Public License as published by the Free 5 | * Software Foundation; either version 3 of the License, or (at your option) 6 | * any later version. 7 | * This library is distributed in the hope that it will be useful, but WITHOUT 8 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 9 | * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 10 | * details. 11 | * You should have received a copy of the GNU Lesser General Public License 12 | * along with this library. If not, see . 13 | */ 14 | 15 | #ifndef __LOCKFREE_LITE_HOOKS_H 16 | #define __LOCKFREE_LITE_HOOKS_H 1 17 | 18 | #include "lite-malloc.h" 19 | #include 20 | 21 | namespace lite 22 | { 23 | 24 | void * malloc_hook (size_t size, void const *) __attribute__ ((malloc)); 25 | 26 | void * malloc_hook (size_t size, void const *) 27 | { 28 | return lockfree::singleton ().do_malloc (size); 29 | } 30 | 31 | void free_hook (void *p, void const *) 32 | { 33 | lockfree::singleton ().do_free (p); 34 | } 35 | 36 | void * realloc_hook (void *p, size_t size, void const *) 37 | { 38 | return lockfree::singleton ().do_realloc (p, size); 39 | } 40 | 41 | void * memalign_hook (size_t align, size_t size, void const *caller) 42 | { 43 | return lockfree::singleton ().do_memalign (align, size); 44 | } 45 | 46 | static void init_hook () 47 | { 48 | __malloc_hook = malloc_hook; 49 | __realloc_hook = realloc_hook; 50 | __free_hook = free_hook; 51 | __memalign_hook = memalign_hook; 52 | } 53 | 54 | } //namespace lite 55 | 56 | void (*__malloc_initialize_hook) (void) = lite::init_hook; 57 | 58 | #endif //__LOCKFREE_LITE_HOOKS_H 59 | -------------------------------------------------------------------------------- /lite-malloc.cpp: -------------------------------------------------------------------------------- 1 | /* Copyright 2011 ZAO "Begun". 2 | * 3 | * This library is free software; you can redistribute it and/or modify it under 4 | * the terms of the GNU Lesser General Public License as published by the Free 5 | * Software Foundation; either version 3 of the License, or (at your option) 6 | * any later version. 7 | * This library is distributed in the hope that it will be useful, but WITHOUT 8 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 9 | * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 10 | * details. 11 | * You should have received a copy of the GNU Lesser General Public License 12 | * along with this library. If not, see . 13 | */ 14 | 15 | #include "lite-hooks-wrap.h" 16 | 17 | -------------------------------------------------------------------------------- /lite-malloc.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2011 ZAO "Begun". 2 | * 3 | * This library is free software; you can redistribute it and/or modify it under 4 | * the terms of the GNU Lesser General Public License as published by the Free 5 | * Software Foundation; either version 3 of the License, or (at your option) 6 | * any later version. 7 | * This library is distributed in the hope that it will be useful, but WITHOUT 8 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 9 | * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 10 | * details. 11 | * You should have received a copy of the GNU Lesser General Public License 12 | * along with this library. If not, see . 13 | */ 14 | 15 | #ifndef __LOCKFREE_LITE_MALLOC_H 16 | #define __LOCKFREE_LITE_MALLOC_H 1 17 | 18 | 19 | // Mmap blocks less then LITE_MALLOC_SUPERBLOCK_CACHE_SIZE * 4096 bytes in size 20 | // will be cached. 21 | 22 | #ifndef LITE_MALLOC_SUPERBLOCK_CACHE_SIZE 23 | #define LITE_MALLOC_SUPERBLOCK_CACHE_SIZE 32 24 | #endif 25 | 26 | // Blocks less then LITE_MALLOC_MINIBLOCK_CACHE_SIZE * 8 bytes in size 27 | // will be cached in pool allocator. 28 | 29 | #ifndef LITE_MALLOC_MINIBLOCK_CACHE_SIZE 30 | #define LITE_MALLOC_MINIBLOCK_CACHE_SIZE 1024 31 | #endif 32 | 33 | // Use LITE_MALLOC_ENGINES_COUNT independent allocator engines. 34 | 35 | #ifndef LITE_MALLOC_ENGINES_COUNT 36 | #define LITE_MALLOC_ENGINES_COUNT 32 37 | #endif 38 | 39 | // Use per-thread engines instead of picking engines uniformly round-robin. 40 | 41 | #ifndef LITE_MALLOC_USE_PERTHREAD_ENGINES 42 | #define LITE_MALLOC_USE_PERTHREAD_ENGINES 1 43 | #endif 44 | 45 | 46 | #include 47 | #include 48 | #include 49 | #include 50 | 51 | #include "aux_.h" 52 | #include "l-singleton.h" 53 | #include "stack.h" 54 | 55 | #ifdef __FreeBSD__ 56 | #define __MAP_ANONYMOUS MAP_ANON 57 | #else 58 | #define __MAP_ANONYMOUS MAP_ANONYMOUS 59 | #endif 60 | 61 | 62 | namespace lite 63 | { 64 | 65 | struct Sb 66 | { 67 | struct Pool *home; 68 | size_t data_size; 69 | size_t mmap_size; 70 | void* mmap_addr; 71 | 72 | size_t engine_id; 73 | char* raw_addr; 74 | size_t _pad2; 75 | size_t _pad3; 76 | 77 | char *raw () 78 | { 79 | return raw_addr; 80 | } 81 | 82 | void set_raw_addr (size_t alignment) 83 | { 84 | char* r = &this->last_one; 85 | size_t n = (size_t)r & alignment; 86 | 87 | if (n) { 88 | r += alignment + 1 - n; 89 | } 90 | 91 | raw_addr = r; 92 | } 93 | 94 | // 32 byte aligned 95 | char last_one; 96 | }; 97 | 98 | size_t const header_size = offsetof(Sb, last_one); 99 | 100 | size_t const grid_step_log2 = 18; 101 | size_t const grid_step = 1 << grid_step_log2; 102 | size_t const offset_mask = grid_step - 1; 103 | size_t const grid_mask = ~offset_mask; 104 | size_t const sb_size = grid_step - lf::base_page; 105 | 106 | inline 107 | size_t grid_align (size_t s) 108 | { 109 | return ((s - 1) & grid_mask) + grid_step; 110 | } 111 | 112 | 113 | struct Sb_cache 114 | { 115 | Sb_cache () {} 116 | 117 | Sb *pop (Pool *home, size_t size, size_t alignment, size_t engine_id) 118 | { 119 | size_t sz = align_up (size, lf::base_page); 120 | 121 | size_t mmap_size = 0; 122 | void* mmap_addr = NULL; 123 | Sb *sb = (Sb*) aux_pop (sz, mmap_addr, mmap_size); 124 | 125 | if (sb) 126 | { 127 | sb->engine_id = engine_id; 128 | sb->home = home; 129 | sb->data_size = sz; 130 | sb->mmap_size = mmap_size; 131 | sb->mmap_addr = mmap_addr; 132 | sb->set_raw_addr (alignment); 133 | } 134 | 135 | return sb; 136 | } 137 | 138 | void push (Sb *sb) 139 | { 140 | size_t off = cache_offset (sb->data_size); 141 | if (off < caches_count) 142 | caches [off].push ((lf::Link *) sb); 143 | else 144 | munmap (sb->mmap_addr, sb->mmap_size); 145 | } 146 | 147 | 148 | private: 149 | 150 | static size_t const caches_count = LITE_MALLOC_SUPERBLOCK_CACHE_SIZE; 151 | lf::Stack caches [caches_count]; 152 | 153 | static size_t cache_offset (size_t size) {return size / lf::base_page - 1;} 154 | 155 | void *aux_pop (size_t size, void*& mmap_addr, size_t& mmap_size) 156 | { 157 | size_t off = cache_offset (size); 158 | if (off < caches_count) 159 | if (void *p = caches [off].pop ()) 160 | return p; 161 | 162 | size_t sx = size + grid_step; 163 | 164 | char *p = map (0, sx); 165 | if (!p) 166 | return 0; 167 | 168 | size_t pp = (size_t) p, ga = grid_align (pp); //, head = ga - pp, tail = sx - size - head; 169 | 170 | char *np = (char *) ga; 171 | 172 | mmap_addr = p; 173 | mmap_size = sx; 174 | 175 | return (Sb*) np; 176 | } 177 | 178 | static 179 | char *map (void *hint, size_t size) 180 | { 181 | void * p = mmap (hint, 182 | size, 183 | PROT_WRITE | PROT_READ, 184 | MAP_PRIVATE | __MAP_ANONYMOUS | (hint ? MAP_FIXED : 0), 185 | -1 /*fd*/, 186 | 0); 187 | 188 | char * ret = (char *) ((long) p == -1 ? 0 : p); 189 | 190 | #ifdef MADV_NOHUGEPAGE 191 | if (ret) { 192 | madvise (ret, size, MADV_NOHUGEPAGE); 193 | } 194 | #endif 195 | return ret; 196 | } 197 | 198 | }; 199 | 200 | //TODO: split exact/inexact pool 201 | struct __attribute__ ((aligned(32))) Pool 202 | { 203 | Pool () : raw (0), elem_size (0) {} 204 | 205 | size_t push (void *p) 206 | { 207 | freed.push ((lf::Link*) p); 208 | return elem_size; 209 | } 210 | 211 | void *pop (Sb_cache &cache, size_t alignment, size_t engine_id) 212 | { 213 | return aux_pop (cache, alignment, engine_id); 214 | } 215 | 216 | 217 | private: 218 | 219 | char *aux_pop (Sb_cache &cache, size_t alignment, size_t engine_id) 220 | { 221 | Sb *sb = 0; 222 | char *result = 0; 223 | char *r = raw.load(std::memory_order_relaxed); 224 | 225 | for ( ; ; ) 226 | { 227 | result = (char *) freed.pop (); 228 | if (result) 229 | break; 230 | 231 | char *next = r + elem_size; 232 | size_t off = (size_t) r & offset_mask; 233 | if (r == 0 || off == 0 || off + elem_size > sb_size) 234 | { 235 | if (!sb) 236 | sb = cache.pop (this, sb_size, alignment, engine_id); 237 | 238 | if (raw.compare_exchange_weak(r, sb->raw () + elem_size, std::memory_order_release, std::memory_order_relaxed)) 239 | { 240 | result = sb->raw (); 241 | sb = 0; 242 | break; 243 | } 244 | } 245 | else if (raw.compare_exchange_weak(r, next, std::memory_order_release, std::memory_order_relaxed)) 246 | { 247 | result = r; 248 | break; 249 | } 250 | } 251 | 252 | if (sb) 253 | cache.push (sb); 254 | 255 | return result; 256 | } 257 | 258 | public: 259 | 260 | lf::Stack freed; 261 | std::atomic raw; 262 | size_t elem_size; 263 | }; 264 | 265 | 266 | inline 267 | size_t block_size (void *p) 268 | { 269 | if (p) 270 | { 271 | Sb *sb = (Sb *) ((size_t) p & grid_mask); 272 | return sb->home ? sb->home->elem_size : sb->data_size - header_size; 273 | } 274 | 275 | return 0; 276 | } 277 | 278 | inline 279 | size_t block_engine_n (void *p) 280 | { 281 | if (p) 282 | { 283 | Sb *sb = (Sb *) ((size_t) p & grid_mask); 284 | return sb->engine_id; 285 | } 286 | 287 | return 0; 288 | } 289 | 290 | class Engine 291 | { 292 | Sb_cache sb_cache; 293 | 294 | static size_t const pools_count = LITE_MALLOC_MINIBLOCK_CACHE_SIZE - 1; //up to 8Kb 295 | Pool pools [pools_count]; 296 | 297 | Engine(Engine&) = delete; 298 | Engine(const Engine&) = delete; 299 | Engine& operator=(Engine&) = delete; 300 | Engine& operator=(const Engine&) = delete; 301 | 302 | public: 303 | 304 | size_t engine_id; 305 | size_t alignment; 306 | 307 | Engine () : engine_id(0), alignment(0) 308 | { 309 | for (size_t i = 0; i < pools_count; ++i) 310 | pools [i].elem_size = (i + 1) * lf::ptr_size; 311 | 312 | //TODO: run GC thread 313 | } 314 | 315 | void *do_malloc (size_t size) 316 | { 317 | if (!size) 318 | return 0; 319 | 320 | size_t words = (size + lf::ptr_size - 1) / lf::ptr_size, off = words - 1; 321 | if (off < pools_count) 322 | return pools [off].pop (sb_cache, alignment, engine_id); 323 | 324 | Sb *sb = sb_cache.pop (0, size + header_size + alignment, alignment, engine_id); 325 | if (!sb) 326 | return 0; 327 | return sb->raw (); 328 | } 329 | 330 | void do_free (void *p) 331 | { 332 | if (p) 333 | { 334 | Sb *sb = (Sb *) ((size_t) p & grid_mask); 335 | if (sb->home) 336 | sb->home->push (p); 337 | else 338 | sb_cache.push (sb); 339 | } 340 | } 341 | 342 | void *do_calloc (size_t n, size_t m) 343 | { 344 | if (size_t size = n * m) 345 | { 346 | void *p = do_malloc (size); 347 | bzero (p, size); 348 | return p; 349 | } 350 | 351 | return 0; 352 | } 353 | 354 | void *do_realloc (void *p, size_t size) 355 | { 356 | if (!p) 357 | return do_malloc (size); 358 | 359 | void *r = 0; 360 | if (size) 361 | { 362 | size_t old_size = block_size (p); 363 | 364 | if (size <= old_size) 365 | return p; 366 | 367 | r = do_malloc (size); 368 | if (!r) 369 | return 0; 370 | 371 | memcpy (r, p, old_size < size ? old_size : size); 372 | } 373 | 374 | do_free (p); 375 | return r; 376 | } 377 | 378 | }; 379 | 380 | constexpr size_t log2(size_t n) 381 | { 382 | return (n <= 1 ? 0 : 1 + log2(n / 2)); 383 | } 384 | 385 | #if LITE_MALLOC_USE_PERTHREAD_ENGINES 386 | #define STATIC_THREAD_LOCAL static thread_local 387 | #else 388 | #define STATIC_THREAD_LOCAL 389 | #endif 390 | 391 | class EnginePool 392 | { 393 | 394 | static size_t const plain_engines_count = LITE_MALLOC_ENGINES_COUNT; 395 | static size_t const aligned_engines_count = log2(lf::base_page) - 3; 396 | static size_t const total_engines_count = plain_engines_count + aligned_engines_count; 397 | Engine engines [total_engines_count]; 398 | 399 | std::atomic alloc_count; 400 | 401 | EnginePool(EnginePool&) = delete; 402 | EnginePool(const EnginePool&) = delete; 403 | EnginePool& operator=(EnginePool&) = delete; 404 | EnginePool& operator=(const EnginePool&) = delete; 405 | 406 | public: 407 | 408 | EnginePool () : alloc_count(0) 409 | { 410 | for (size_t i = 0; i < total_engines_count; ++i) 411 | engines [i].engine_id = i + 1; 412 | 413 | for (size_t i = plain_engines_count, a = 16; i < total_engines_count; ++i, a *= 2) 414 | engines [i].alignment = a - 1; 415 | } 416 | 417 | void *do_malloc (size_t size) 418 | { 419 | STATIC_THREAD_LOCAL size_t en{++alloc_count % plain_engines_count}; 420 | 421 | return engines [en].do_malloc(size); 422 | } 423 | 424 | void do_free (void *p) 425 | { 426 | size_t engine = block_engine_n(p); 427 | 428 | if (engine) 429 | engines [engine-1].do_free(p); 430 | } 431 | 432 | void *do_calloc (size_t n, size_t m) 433 | { 434 | STATIC_THREAD_LOCAL size_t en{++alloc_count % plain_engines_count}; 435 | 436 | return engines [en].do_calloc(n, m); 437 | } 438 | 439 | void *do_realloc (void *p, size_t size) 440 | { 441 | STATIC_THREAD_LOCAL size_t en{++alloc_count % plain_engines_count}; 442 | size_t engine = block_engine_n(p); 443 | 444 | if (!engine) 445 | engine = en; 446 | else 447 | engine = engine - 1; 448 | 449 | return engines [engine].do_realloc(p, size); 450 | } 451 | 452 | void *do_memalign (size_t align, size_t size) 453 | { 454 | if (align <= 8) 455 | return do_malloc (size); 456 | 457 | size = align_up (size, align); 458 | align--; 459 | 460 | for (size_t i = plain_engines_count; i < total_engines_count; ++i) 461 | { 462 | if (engines [i].alignment == align) 463 | { 464 | return engines [i].do_malloc(size); 465 | } 466 | } 467 | 468 | return 0; 469 | } 470 | }; 471 | 472 | #undef STATIC_THREAD_LOCAL 473 | 474 | } //namespace lite 475 | 476 | #endif //__LOCKFREE_LITE_MALLOC_H 477 | -------------------------------------------------------------------------------- /stack.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2011 ZAO "Begun". 2 | * 3 | * This library is free software; you can redistribute it and/or modify it under 4 | * the terms of the GNU Lesser General Public License as published by the Free 5 | * Software Foundation; either version 3 of the License, or (at your option) 6 | * any later version. 7 | * This library is distributed in the hope that it will be useful, but WITHOUT 8 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 9 | * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 10 | * details. 11 | * You should have received a copy of the GNU Lesser General Public License 12 | * along with this library. If not, see . 13 | */ 14 | 15 | // $Id$ 16 | 17 | #ifndef LOCKFREE_STACK_H 18 | #define LOCKFREE_STACK_H 1 19 | 20 | #include 21 | 22 | namespace lockfree 23 | { 24 | 25 | template 26 | struct Stack 27 | { 28 | Stack () 29 | { 30 | Head const h = { 0, 0 }; 31 | m_head.store(h, std::memory_order_relaxed); 32 | } 33 | 34 | T* 35 | pop () 36 | { 37 | Head old = m_head.load(std::memory_order_relaxed); 38 | 39 | for ( ; ; ) 40 | { 41 | if (ptr (old) == NULL) 42 | return NULL; 43 | 44 | Head const h = {(long) ptr (old)->next, old.tag}; 45 | 46 | if (m_head.compare_exchange_weak(old, h, std::memory_order_release, std::memory_order_relaxed)) 47 | { 48 | return ptr (old); 49 | } 50 | } 51 | } 52 | 53 | void 54 | push (T* obj) 55 | { 56 | Head old = m_head.load(std::memory_order_relaxed); 57 | for ( ; ; ) 58 | { 59 | Head const h = {(long) obj, old.tag + (size_t)1}; 60 | obj->next = ptr (old); 61 | 62 | if (m_head.compare_exchange_weak(old, h, std::memory_order_release, std::memory_order_relaxed)) 63 | return; 64 | } 65 | } 66 | 67 | private: 68 | 69 | struct Head 70 | { 71 | long ptr : 48; 72 | size_t tag : 16; 73 | }; 74 | 75 | std::atomic m_head; 76 | 77 | static 78 | T* 79 | ptr (Head const h) 80 | { 81 | return (T*)(long) h.ptr; 82 | } 83 | }; 84 | 85 | } //namespace lockfree 86 | 87 | #endif //LOCKFREE_STACK_H 88 | --------------------------------------------------------------------------------