Changes in / [6b4a1bf:9c6f459]
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
doc/user/user.tex
r6b4a1bf r9c6f459 11 11 %% Created On : Wed Apr 6 14:53:29 2016 12 12 %% Last Modified By : Peter A. Buhr 13 %% Last Modified On : Fri Mar 6 13:34:52 202014 %% Update Count : 3 92413 %% Last Modified On : Thu Mar 5 12:09:42 2020 14 %% Update Count : 3885 15 15 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 16 16 … … 6621 6621 An array may be filled, resized, or aligned. 6622 6622 \end{description} 6623 \VRef[Table]{t:AllocationVersusCapabilities} shows allocation routines supporting different combinations of storage-management capabilities .6623 \VRef[Table]{t:AllocationVersusCapabilities} shows allocation routines supporting different combinations of storage-management capabilities: 6624 6624 \begin{table} 6625 6625 \centering 6626 \begin{minipage}{0.75\textwidth}6627 6626 \begin{tabular}{@{}r|l|l|l|l|l@{}} 6628 6627 \multicolumn{1}{c}{}& & \multicolumn{1}{c|}{fill} & resize & alignment & array \\ … … 6632 6631 & ©realloc© & copy & yes & no & no \\ 6633 6632 & ©memalign© & no & no & yes & no \\ 6634 & ©aligned_alloc©\footnote{Same as ©memalign© but size is an integral multiple of alignment, which is universally ignored.} 6635 & no & no & yes & no \\ 6633 & ©aligned_alloc© & no & no & yes & no \\ 6636 6634 & ©posix_memalign© & no & no & yes & no \\ 6637 & ©valloc© & no & no & yes (page size)& no \\6638 & ©pvalloc©\footnote{Same as ©valloc© but rounds size to multiple of page size.}6639 & no & no & yes (page size)& no \\6640 6635 \hline 6641 6636 \CFA & ©cmemalign© & yes (0 only) & no & yes & yes \\ 6642 6637 & ©realloc© & copy & yes & yes & no \\ 6643 & ©alloc© & no & yes & no & yes \\ 6644 & ©alloc_set© & yes & yes & no & yes \\ 6645 & ©alloc_align© & no & yes & yes & yes \\ 6646 & ©alloc_align_set© & yes & yes & yes & yes \\ 6638 & ©alloc© & no & no & no & no \\ 6639 & ©alloc© & copy & no/yes & no & yes \\ 6640 & ©alloc© & no/copy/yes & no/yes & no & yes \\ 6641 & ©alloc_set© & no/yes & no & yes & yes \\ 6642 & ©alloc_align© & no/yes & no & yes & yes \\ 6643 & ©alloc_align_set© & no/yes & no & yes & yes \\ 6647 6644 \end{tabular} 6648 \end{minipage}6649 6645 \caption{Allocation Routines versus Storage-Management Capabilities} 6650 6646 \label{t:AllocationVersusCapabilities} 6651 6647 \end{table} 6652 6653 \CFA memory management extends the type safety of all allocations by using the type of the left-hand-side type to determine the allocation size and return a matching type for the new storage.6654 Type-safe allocation is provided for all C allocation routines and new \CFA allocation routines, \eg in6655 \begin{cfa}6656 int * ip = (int *)malloc( sizeof(int) ); §\C{// C}§6657 int * ip = malloc(); §\C{// \CFA type-safe version of C malloc}§6658 int * ip = alloc(); §\C{// \CFA type-safe uniform alloc}§6659 \end{cfa}6660 the latter two allocations determine the allocation size from the type of ©p© (©int©) and cast the pointer to the allocated storage to ©int *©.6661 6662 \CFA memory management extends allocation safety by implicitly honouring all alignment requirements, \eg in6663 \begin{cfa}6664 struct S { int i; } __attribute__(( aligned( 128 ) )); // cache-line alignment6665 S * sp = malloc(); §\C{// honour type alignment}§6666 \end{cfa}6667 the storage allocation is implicitly aligned to 128 rather than the default 16.6668 The alignment check is performed at compile time so there is no runtime cost.6669 6648 6670 6649 \CFA memory management extends the resize capability with the notion of \newterm{sticky properties}. … … 6673 6652 Without sticky properties it is dangerous to use ©realloc©, resulting in an idiom of manually performing the reallocation to maintain correctness. 6674 6653 6675 \CFA memory management extends allocation to support constructors for initialization of allocated storage, \eg in6676 \begin{cfa}6677 struct S { int i; }; §\C{// cache-line aglinment}§6678 void ?{}( S & s, int i ) { s.i = i; }6679 // assume ?|? operator for printing an S6680 6681 S & sp = *®new®( 3 ); §\C{// call constructor after allocation}§6682 sout | sp.i;6683 ®delete®( &sp );6684 6685 S * spa = ®anew®( 10, 5 ); §\C{// allocate array and initialize each array element}§6686 for ( i; 10 ) sout | spa[i] | nonl;6687 sout | nl;6688 ®adelete®( 10, spa );6689 \end{cfa}6690 Allocation routines ©new©/©anew© allocate a variable/array and initialize storage using the allocated type's constructor.6691 Note, the matching deallocation routines ©delete©/©adelete©.6692 6654 6693 6655 \leavevmode -
libcfa/src/heap.cfa
r6b4a1bf r9c6f459 10 10 // Created On : Tue Dec 19 21:58:35 2017 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Fri Mar 6 10:14:52202013 // Update Count : 6 5012 // Last Modified On : Tue Feb 4 10:04:51 2020 13 // Update Count : 648 14 14 // 15 15 … … 819 819 820 820 extern "C" { 821 // Allocates size bytes and returns a pointer to the allocated memory. The memory is not initialized. If size is 0, 822 // then malloc() returns either 0p, or a unique pointer value that can later be successfully passed to free(). 821 // The malloc() function allocates size bytes and returns a pointer to the allocated memory. The memory is not 822 // initialized. If size is 0, then malloc() returns either 0p, or a unique pointer value that can later be 823 // successfully passed to free(). 823 824 void * malloc( size_t size ) { 824 825 #ifdef __STATISTICS__ … … 830 831 } // malloc 831 832 832 // Allocate memory for an array of nmemb elements of size bytes each and returns a pointer to the allocated833 // memory. The memory is set to zero. If nmemb or size is 0, then calloc() returns either 0p, or a unique pointer834 // value that can later be successfully passed to free().833 // The calloc() function allocates memory for an array of nmemb elements of size bytes each and returns a pointer to 834 // the allocated memory. The memory is set to zero. If nmemb or size is 0, then calloc() returns either 0p, or a 835 // unique pointer value that can later be successfully passed to free(). 835 836 void * calloc( size_t noOfElems, size_t elemSize ) { 836 837 #ifdef __STATISTICS__ … … 842 843 } // calloc 843 844 844 // Change the size of the memory block pointed to by ptr to size bytes. The contents shall be unchanged in the range845 // from the start of the region up to the minimum of the old and new sizes. If the new size is larger than the old846 // size, the added memory shall not be initialized. If ptr is 0p, then the call is equivalent to malloc(size), for847 // all values of size; if size is equal to zero, and ptr is not 0p, then the call is equivalent to free(ptr). Unless848 // ptr is 0p, it must have been returned by an earlier call to malloc(), calloc() or realloc(). If the area pointed849 // to was moved, a free(ptr) is done.845 // The realloc() function changes the size of the memory block pointed to by ptr to size bytes. The contents will be 846 // unchanged in the range from the start of the region up to the minimum of the old and new sizes. If the new size 847 // is larger than the old size, the added memory will not be initialized. If ptr is 0p, then the call is 848 // equivalent to malloc(size), for all values of size; if size is equal to zero, and ptr is not 0p, then the call 849 // is equivalent to free(ptr). Unless ptr is 0p, it must have been returned by an earlier call to malloc(), 850 // calloc() or realloc(). If the area pointed to was moved, a free(ptr) is done. 850 851 void * realloc( void * oaddr, size_t size ) { 851 852 #ifdef __STATISTICS__ … … 902 903 } // realloc 903 904 904 // Allocates size bytes and returns a pointer to the allocated memory. The memory address shall be a multiple of905 // a lignment, which must be a power of two. (obsolete)905 // The obsolete function memalign() allocates size bytes and returns a pointer to the allocated memory. The memory 906 // address will be a multiple of alignment, which must be a power of two. 906 907 void * memalign( size_t alignment, size_t size ) { 907 908 #ifdef __STATISTICS__ … … 914 915 915 916 916 // Same as calloc() with memory alignment.917 // The cmemalign() function is the same as calloc() with memory alignment. 917 918 void * cmemalign( size_t alignment, size_t noOfElems, size_t elemSize ) { 918 919 #ifdef __STATISTICS__ … … 924 925 } // cmemalign 925 926 926 // Same as memalign(), but ISO/IEC 2011 C11 Section 7.22.2 states: the value of size shall be an integral multiple927 // of alignment. This requirement is universally ignored.927 // The function aligned_alloc() is the same as memalign(), except for the added restriction that size should be a 928 // multiple of alignment. 928 929 void * aligned_alloc( size_t alignment, size_t size ) { 929 930 return memalign( alignment, size ); … … 931 932 932 933 933 // Allocates size bytes and places the address of the allocated memory in *memptr. The address of the allocated934 // memory shall be a multiple of alignment, which must be a power of two and a multiple of sizeof(void *). If size935 // is 0, then posix_memalign() returns either 0p, or a unique pointer value that can later be successfully passed to936 // free(3).934 // The function posix_memalign() allocates size bytes and places the address of the allocated memory in *memptr. The 935 // address of the allocated memory will be a multiple of alignment, which must be a power of two and a multiple of 936 // sizeof(void *). If size is 0, then posix_memalign() returns either 0p, or a unique pointer value that can later 937 // be successfully passed to free(3). 937 938 int posix_memalign( void ** memptr, size_t alignment, size_t size ) { 938 939 if ( alignment < sizeof(void *) || ! libPow2( alignment ) ) return EINVAL; // check alignment … … 942 943 } // posix_memalign 943 944 944 // Allocates size bytes and returns a pointer to the allocated memory. The memory address shall be a multiple of the945 // page size. It is equivalent to memalign(sysconf(_SC_PAGESIZE),size).945 // The obsolete function valloc() allocates size bytes and returns a pointer to the allocated memory. The memory 946 // address will be a multiple of the page size. It is equivalent to memalign(sysconf(_SC_PAGESIZE),size). 946 947 void * valloc( size_t size ) { 947 948 return memalign( pageSize, size ); … … 949 950 950 951 951 // Same as valloc but rounds size to multiple of page size. 952 void * pvalloc( size_t size ) { 953 return memalign( pageSize, libCeiling( size, pageSize ) ); 954 } // pvalloc 955 956 957 // Frees the memory space pointed to by ptr, which must have been returned by a previous call to malloc(), calloc() 958 // or realloc(). Otherwise, or if free(ptr) has already been called before, undefined behavior occurs. If ptr is 959 // 0p, no operation is performed. 952 // The free() function frees the memory space pointed to by ptr, which must have been returned by a previous call to 953 // malloc(), calloc() or realloc(). Otherwise, or if free(ptr) has already been called before, undefined behavior 954 // occurs. If ptr is 0p, no operation is performed. 960 955 void free( void * addr ) { 961 956 #ifdef __STATISTICS__ … … 978 973 979 974 980 // Returns the alignment of the allocation.975 // The malloc_alignment() function returns the alignment of the allocation. 981 976 size_t malloc_alignment( void * addr ) { 982 977 if ( unlikely( addr == 0p ) ) return libAlign(); // minimum alignment … … 990 985 991 986 992 // Returns true if the allocation is zero filled, i.e., initially allocated by calloc().987 // The malloc_zero_fill() function returns true if the allocation is zero filled, i.e., initially allocated by calloc(). 993 988 bool malloc_zero_fill( void * addr ) { 994 989 if ( unlikely( addr == 0p ) ) return false; // null allocation is not zero fill … … 1001 996 1002 997 1003 // Returns the number of usable bytes in the block pointed to by ptr, a pointer to a block of memory allocated by1004 // mallocor a related function.998 // The malloc_usable_size() function returns the number of usable bytes in the block pointed to by ptr, a pointer to 999 // a block of memory allocated by malloc(3) or a related function. 1005 1000 size_t malloc_usable_size( void * addr ) { 1006 1001 if ( unlikely( addr == 0p ) ) return 0; // null allocation has 0 size … … 1014 1009 1015 1010 1016 // Prints (on default standard error) statistics about memory allocated by malloc and related functions. 1011 // The malloc_stats() function prints (on default standard error) statistics about memory allocated by malloc(3) and 1012 // related functions. 1017 1013 void malloc_stats( void ) { 1018 1014 #ifdef __STATISTICS__ … … 1022 1018 } // malloc_stats 1023 1019 1024 // Changes the file descripter where malloc_stats() writesstatistics.1020 // The malloc_stats_fd() function changes the file descripter where malloc_stats() writes the statistics. 1025 1021 int malloc_stats_fd( int fd __attribute__(( unused )) ) { 1026 1022 #ifdef __STATISTICS__ … … 1034 1030 1035 1031 1036 // Adjusts parameters that control the behavior of the memory-allocation functions (see malloc). The param argument 1037 // specifies the parameter to be modified, and value specifies the new value for that parameter. 1032 // The mallopt() function adjusts parameters that control the behavior of the memory-allocation functions (see 1033 // malloc(3)). The param argument specifies the parameter to be modified, and value specifies the new value for that 1034 // parameter. 1038 1035 int mallopt( int option, int value ) { 1039 1036 choose( option ) { … … 1046 1043 } // mallopt 1047 1044 1048 // Attempt to release free memory at the top of the heap (by calling sbrk with a suitable argument). 1045 // The malloc_trim() function attempts to release free memory at the top of the heap (by calling sbrk(2) with a 1046 // suitable argument). 1049 1047 int malloc_trim( size_t ) { 1050 1048 return 0; // => impossible to release memory … … 1052 1050 1053 1051 1054 // Exports an XML string that describes the current state of the memory-allocation implementation in the caller.1055 // The string is printed on the file stream stream. The exported string includes information about all arenas (see1056 // malloc).1052 // The malloc_info() function exports an XML string that describes the current state of the memory-allocation 1053 // implementation in the caller. The string is printed on the file stream stream. The exported string includes 1054 // information about all arenas (see malloc(3)). 1057 1055 int malloc_info( int options, FILE * stream ) { 1058 1056 if ( options != 0 ) { errno = EINVAL; return -1; } … … 1061 1059 1062 1060 1063 // Records the current state of all malloc internal bookkeeping variables (but not the actual contents of the heap1064 // or the state of malloc_hook functions pointers). The state is recorded in a system-dependent opaque data1065 // structure dynamically allocated via malloc, and a pointer to that data structure is returned as the function1066 // result. (The caller must freethis memory.)1061 // The malloc_get_state() function records the current state of all malloc(3) internal bookkeeping variables (but 1062 // not the actual contents of the heap or the state of malloc_hook(3) functions pointers). The state is recorded in 1063 // a system-dependent opaque data structure dynamically allocated via malloc(3), and a pointer to that data 1064 // structure is returned as the function result. (It is the caller's responsibility to free(3) this memory.) 1067 1065 void * malloc_get_state( void ) { 1068 1066 return 0p; // unsupported … … 1070 1068 1071 1069 1072 // Restores the state of all malloc internal bookkeeping variables to the values recorded in the opaque data1073 // structure pointed to by state.1070 // The malloc_set_state() function restores the state of all malloc(3) internal bookkeeping variables to the values 1071 // recorded in the opaque data structure pointed to by state. 1074 1072 int malloc_set_state( void * ptr ) { 1075 1073 return 0; // unsupported -
libcfa/src/stdlib.hfa
r6b4a1bf r9c6f459 10 10 // Created On : Thu Jan 28 17:12:35 2016 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : T hu Mar 5 11:29:06202013 // Update Count : 40 712 // Last Modified On : Tue Feb 4 08:27:01 2020 13 // Update Count : 401 14 14 // 15 15 … … 21 21 #include <stdlib.h> // *alloc, strto*, ato* 22 22 23 // Reduce includes by explicitly defining these routines.24 23 extern "C" { 25 24 void * memalign( size_t align, size_t size ); // malloc.h 26 void * cmemalign( size_t alignment, size_t noOfElems, size_t elemSize ); // CFA heap27 25 void * memset( void * dest, int fill, size_t size ); // string.h 28 26 void * memcpy( void * dest, const void * src, size_t size ); // string.h 27 void * cmemalign( size_t alignment, size_t noOfElems, size_t elemSize ); // CFA heap 29 28 } // extern "C" 30 29 … … 41 40 42 41 static inline forall( dtype T | sized(T) ) { 43 // C forall safe equivalents, i.e., implicit size specification42 // C dynamic allocation 44 43 45 44 T * malloc( void ) { … … 73 72 } // posix_memalign 74 73 75 // Cforall safe general allocation, fill, resize, array74 // Cforall dynamic allocation 76 75 77 76 T * alloc( void ) { … … 160 159 161 160 static inline forall( dtype T | sized(T) ) { 162 // Cforall safe initialization/copy, i.e., implicit size specification, non-array types161 // data, non-array types 163 162 T * memset( T * dest, char fill ) { 164 163 return (T *)memset( dest, fill, sizeof(T) ); … … 171 170 172 171 static inline forall( dtype T | sized(T) ) { 173 // Cforall safe initialization/copy, i.e., implicit size specification, array types172 // data, array types 174 173 T * amemset( T dest[], char fill, size_t dim ) { 175 174 return (T *)(void *)memset( dest, fill, dim * sizeof(T) ); // C memset … … 181 180 } // distribution 182 181 183 // Cforallallocation/deallocation and constructor/destructor, non-array types182 // allocation/deallocation and constructor/destructor, non-array types 184 183 forall( dtype T | sized(T), ttype Params | { void ?{}( T &, Params ); } ) T * new( Params p ); 185 184 forall( dtype T | sized(T) | { void ^?{}( T & ); } ) void delete( T * ptr ); 186 185 forall( dtype T, ttype Params | sized(T) | { void ^?{}( T & ); void delete( Params ); } ) void delete( T * ptr, Params rest ); 187 186 188 // Cforallallocation/deallocation and constructor/destructor, array types187 // allocation/deallocation and constructor/destructor, array types 189 188 forall( dtype T | sized(T), ttype Params | { void ?{}( T &, Params ); } ) T * anew( size_t dim, Params p ); 190 189 forall( dtype T | sized(T) | { void ^?{}( T & ); } ) void adelete( size_t dim, T arr[] );
Note: See TracChangeset
for help on using the changeset viewer.