Changes in / [9c6f459:6b4a1bf]
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
doc/user/user.tex
r9c6f459 r6b4a1bf 11 11 %% Created On : Wed Apr 6 14:53:29 2016 12 12 %% Last Modified By : Peter A. Buhr 13 %% Last Modified On : Thu Mar 5 12:09:42 202014 %% Update Count : 3 88513 %% Last Modified On : Fri Mar 6 13:34:52 2020 14 %% Update Count : 3924 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} 6626 6627 \begin{tabular}{@{}r|l|l|l|l|l@{}} 6627 6628 \multicolumn{1}{c}{}& & \multicolumn{1}{c|}{fill} & resize & alignment & array \\ … … 6631 6632 & ©realloc© & copy & yes & no & no \\ 6632 6633 & ©memalign© & no & no & yes & no \\ 6633 & ©aligned_alloc© & 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 \\ 6634 6636 & ©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 \\ 6635 6640 \hline 6636 6641 \CFA & ©cmemalign© & yes (0 only) & no & yes & yes \\ 6637 6642 & ©realloc© & copy & yes & yes & no \\ 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 \\ 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 \\ 6644 6647 \end{tabular} 6648 \end{minipage} 6645 6649 \caption{Allocation Routines versus Storage-Management Capabilities} 6646 6650 \label{t:AllocationVersusCapabilities} 6647 6651 \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 in 6655 \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 in 6663 \begin{cfa} 6664 struct S { int i; } __attribute__(( aligned( 128 ) )); // cache-line alignment 6665 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. 6648 6669 6649 6670 \CFA memory management extends the resize capability with the notion of \newterm{sticky properties}. … … 6652 6673 Without sticky properties it is dangerous to use ©realloc©, resulting in an idiom of manually performing the reallocation to maintain correctness. 6653 6674 6675 \CFA memory management extends allocation to support constructors for initialization of allocated storage, \eg in 6676 \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 S 6680 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©. 6654 6692 6655 6693 \leavevmode -
libcfa/src/heap.cfa
r9c6f459 r6b4a1bf 10 10 // Created On : Tue Dec 19 21:58:35 2017 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Tue Feb 4 10:04:51202013 // Update Count : 6 4812 // Last Modified On : Fri Mar 6 10:14:52 2020 13 // Update Count : 650 14 14 // 15 15 … … 819 819 820 820 extern "C" { 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(). 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(). 824 823 void * malloc( size_t size ) { 825 824 #ifdef __STATISTICS__ … … 831 830 } // malloc 832 831 833 // The calloc() function allocates memory for an array of nmemb elements of size bytes each and returns a pointer to834 // the allocated memory. The memory is set to zero. If nmemb or size is 0, then calloc() returns either 0p, or a835 // unique pointervalue that can later be successfully passed to free().832 // Allocate memory for an array of nmemb elements of size bytes each and returns a pointer to the allocated 833 // memory. The memory is set to zero. If nmemb or size is 0, then calloc() returns either 0p, or a unique pointer 834 // value that can later be successfully passed to free(). 836 835 void * calloc( size_t noOfElems, size_t elemSize ) { 837 836 #ifdef __STATISTICS__ … … 843 842 } // calloc 844 843 845 // The realloc() function changes the size of the memory block pointed to by ptr to size bytes. The contents will be846 // unchanged in the range from the start of the region up to the minimum of the old and new sizes. If the new size847 // is larger than the old size, the added memory will not be initialized. If ptr is 0p, then the call is848 // equivalent to malloc(size), for all values of size; if size is equal to zero, and ptr is not 0p, then the call849 // 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 pointedto was moved, a free(ptr) is done.844 // Change the size of the memory block pointed to by ptr to size bytes. The contents shall be unchanged in the range 845 // from the start of the region up to the minimum of the old and new sizes. If the new size is larger than the old 846 // size, the added memory shall not be initialized. If ptr is 0p, then the call is equivalent to malloc(size), for 847 // all values of size; if size is equal to zero, and ptr is not 0p, then the call is equivalent to free(ptr). Unless 848 // ptr is 0p, it must have been returned by an earlier call to malloc(), calloc() or realloc(). If the area pointed 849 // to was moved, a free(ptr) is done. 851 850 void * realloc( void * oaddr, size_t size ) { 852 851 #ifdef __STATISTICS__ … … 903 902 } // realloc 904 903 905 // The obsolete function memalign() allocates size bytes and returns a pointer to the allocated memory. The memory906 // a ddress will be a multiple of alignment, which must be a power of two.904 // Allocates size bytes and returns a pointer to the allocated memory. The memory address shall be a multiple of 905 // alignment, which must be a power of two. (obsolete) 907 906 void * memalign( size_t alignment, size_t size ) { 908 907 #ifdef __STATISTICS__ … … 915 914 916 915 917 // The cmemalign() function is the same as calloc() with memory alignment.916 // Same as calloc() with memory alignment. 918 917 void * cmemalign( size_t alignment, size_t noOfElems, size_t elemSize ) { 919 918 #ifdef __STATISTICS__ … … 925 924 } // cmemalign 926 925 927 // The function aligned_alloc() is the same as memalign(), except for the added restriction that size should be a928 // multiple of alignment.926 // Same as memalign(), but ISO/IEC 2011 C11 Section 7.22.2 states: the value of size shall be an integral multiple 927 // of alignment. This requirement is universally ignored. 929 928 void * aligned_alloc( size_t alignment, size_t size ) { 930 929 return memalign( alignment, size ); … … 932 931 933 932 934 // The function posix_memalign() allocates size bytes and places the address of the allocated memory in *memptr. The935 // address of the allocated memory will be a multiple of alignment, which must be a power of two and a multiple of936 // sizeof(void *). If size is 0, then posix_memalign() returns either 0p, or a unique pointer value that can later937 // be successfully passed tofree(3).933 // Allocates size bytes and places the address of the allocated memory in *memptr. The address of the allocated 934 // memory shall be a multiple of alignment, which must be a power of two and a multiple of sizeof(void *). If size 935 // is 0, then posix_memalign() returns either 0p, or a unique pointer value that can later be successfully passed to 936 // free(3). 938 937 int posix_memalign( void ** memptr, size_t alignment, size_t size ) { 939 938 if ( alignment < sizeof(void *) || ! libPow2( alignment ) ) return EINVAL; // check alignment … … 943 942 } // posix_memalign 944 943 945 // The obsolete function valloc() allocates size bytes and returns a pointer to the allocated memory. The memory946 // address will be a multiple of thepage size. It is equivalent to memalign(sysconf(_SC_PAGESIZE),size).944 // Allocates size bytes and returns a pointer to the allocated memory. The memory address shall be a multiple of the 945 // page size. It is equivalent to memalign(sysconf(_SC_PAGESIZE),size). 947 946 void * valloc( size_t size ) { 948 947 return memalign( pageSize, size ); … … 950 949 951 950 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. 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. 955 960 void free( void * addr ) { 956 961 #ifdef __STATISTICS__ … … 973 978 974 979 975 // The malloc_alignment() function returns the alignment of the allocation.980 // Returns the alignment of the allocation. 976 981 size_t malloc_alignment( void * addr ) { 977 982 if ( unlikely( addr == 0p ) ) return libAlign(); // minimum alignment … … 985 990 986 991 987 // The malloc_zero_fill() function returns true if the allocation is zero filled, i.e., initially allocated by calloc().992 // Returns true if the allocation is zero filled, i.e., initially allocated by calloc(). 988 993 bool malloc_zero_fill( void * addr ) { 989 994 if ( unlikely( addr == 0p ) ) return false; // null allocation is not zero fill … … 996 1001 997 1002 998 // The malloc_usable_size() function returns the number of usable bytes in the block pointed to by ptr, a pointer to999 // a block of memory allocated by malloc(3)or a related function.1003 // Returns the number of usable bytes in the block pointed to by ptr, a pointer to a block of memory allocated by 1004 // malloc or a related function. 1000 1005 size_t malloc_usable_size( void * addr ) { 1001 1006 if ( unlikely( addr == 0p ) ) return 0; // null allocation has 0 size … … 1009 1014 1010 1015 1011 // The malloc_stats() function prints (on default standard error) statistics about memory allocated by malloc(3) and 1012 // related functions. 1016 // Prints (on default standard error) statistics about memory allocated by malloc and related functions. 1013 1017 void malloc_stats( void ) { 1014 1018 #ifdef __STATISTICS__ … … 1018 1022 } // malloc_stats 1019 1023 1020 // The malloc_stats_fd() function changes the file descripter where malloc_stats() writes thestatistics.1024 // Changes the file descripter where malloc_stats() writes statistics. 1021 1025 int malloc_stats_fd( int fd __attribute__(( unused )) ) { 1022 1026 #ifdef __STATISTICS__ … … 1030 1034 1031 1035 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. 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. 1035 1038 int mallopt( int option, int value ) { 1036 1039 choose( option ) { … … 1043 1046 } // mallopt 1044 1047 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). 1048 // Attempt to release free memory at the top of the heap (by calling sbrk with a suitable argument). 1047 1049 int malloc_trim( size_t ) { 1048 1050 return 0; // => impossible to release memory … … 1050 1052 1051 1053 1052 // The malloc_info() function exports an XML string that describes the current state of the memory-allocation1053 // implementation in the caller. The string is printed on the file stream stream. The exported string includes1054 // information about all arenas (see malloc(3)).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 (see 1056 // malloc). 1055 1057 int malloc_info( int options, FILE * stream ) { 1056 1058 if ( options != 0 ) { errno = EINVAL; return -1; } … … 1059 1061 1060 1062 1061 // The malloc_get_state() function records the current state of all malloc(3) internal bookkeeping variables (but1062 // not the actual contents of the heap or the state of malloc_hook(3) functions pointers). The state is recorded in1063 // a system-dependent opaque data structure dynamically allocated via malloc(3), and a pointer to that data1064 // structure is returned as the function result. (It is the caller's responsibility to free(3)this memory.)1063 // Records the current state of all malloc internal bookkeeping variables (but not the actual contents of the heap 1064 // or the state of malloc_hook functions pointers). The state is recorded in a system-dependent opaque data 1065 // structure dynamically allocated via malloc, and a pointer to that data structure is returned as the function 1066 // result. (The caller must free this memory.) 1065 1067 void * malloc_get_state( void ) { 1066 1068 return 0p; // unsupported … … 1068 1070 1069 1071 1070 // The malloc_set_state() function restores the state of all malloc(3) internal bookkeeping variables to the values1071 // recorded in the opaque datastructure pointed to by state.1072 // Restores the state of all malloc internal bookkeeping variables to the values recorded in the opaque data 1073 // structure pointed to by state. 1072 1074 int malloc_set_state( void * ptr ) { 1073 1075 return 0; // unsupported -
libcfa/src/stdlib.hfa
r9c6f459 r6b4a1bf 10 10 // Created On : Thu Jan 28 17:12:35 2016 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : T ue Feb 4 08:27:01202013 // Update Count : 40 112 // Last Modified On : Thu Mar 5 11:29:06 2020 13 // Update Count : 407 14 14 // 15 15 … … 21 21 #include <stdlib.h> // *alloc, strto*, ato* 22 22 23 // Reduce includes by explicitly defining these routines. 23 24 extern "C" { 24 25 void * memalign( size_t align, size_t size ); // malloc.h 26 void * cmemalign( size_t alignment, size_t noOfElems, size_t elemSize ); // CFA heap 25 27 void * memset( void * dest, int fill, size_t size ); // string.h 26 28 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 heap28 29 } // extern "C" 29 30 … … 40 41 41 42 static inline forall( dtype T | sized(T) ) { 42 // C dynamic allocation43 // Cforall safe equivalents, i.e., implicit size specification 43 44 44 45 T * malloc( void ) { … … 72 73 } // posix_memalign 73 74 74 // Cforall dynamic allocation75 // Cforall safe general allocation, fill, resize, array 75 76 76 77 T * alloc( void ) { … … 159 160 160 161 static inline forall( dtype T | sized(T) ) { 161 // data, non-array types162 // Cforall safe initialization/copy, i.e., implicit size specification, non-array types 162 163 T * memset( T * dest, char fill ) { 163 164 return (T *)memset( dest, fill, sizeof(T) ); … … 170 171 171 172 static inline forall( dtype T | sized(T) ) { 172 // data, array types173 // Cforall safe initialization/copy, i.e., implicit size specification, array types 173 174 T * amemset( T dest[], char fill, size_t dim ) { 174 175 return (T *)(void *)memset( dest, fill, dim * sizeof(T) ); // C memset … … 180 181 } // distribution 181 182 182 // allocation/deallocation and constructor/destructor, non-array types183 // Cforall allocation/deallocation and constructor/destructor, non-array types 183 184 forall( dtype T | sized(T), ttype Params | { void ?{}( T &, Params ); } ) T * new( Params p ); 184 185 forall( dtype T | sized(T) | { void ^?{}( T & ); } ) void delete( T * ptr ); 185 186 forall( dtype T, ttype Params | sized(T) | { void ^?{}( T & ); void delete( Params ); } ) void delete( T * ptr, Params rest ); 186 187 187 // allocation/deallocation and constructor/destructor, array types188 // Cforall allocation/deallocation and constructor/destructor, array types 188 189 forall( dtype T | sized(T), ttype Params | { void ?{}( T &, Params ); } ) T * anew( size_t dim, Params p ); 189 190 forall( dtype T | sized(T) | { void ^?{}( T & ); } ) void adelete( size_t dim, T arr[] );
Note: See TracChangeset
for help on using the changeset viewer.