Differences From Artifact [eb1f0d3652]:
- File src/OFObject.m — part of check-in [5a7c58ec98] at 2012-03-13 20:19:34 on branch trunk — Add of_num_cpus. (user: js, size: 25566) [annotate] [blame] [check-ins using]
To Artifact [329e3b32bd]:
- File
src/OFObject.m
— part of check-in
[28170f5f65]
at
2012-03-15 11:29:24
on branch trunk
— Greatly improve OFObject's memory handling and performance.
A linked-list is put before each memory chunk allocated instead of
having an array of all memory chunks. This means only one malloc now
instead of one malloc and one realloc. This also means that when
checking reallocs and frees, it's no longer necessary to iterate through
all memory chunks, as the linked list also contains the owner, meaning
realloc and free are no longer O(n), but O(1) now.As allocating bigger chunks seems to be a little bit slower than smaller
chunks, it seems that this is slightly slower in benchmarks if only very small
chunks are allocated. However, measuring real world usage, it's a lot faster. (user: js, size: 24294) [annotate] [blame] [check-ins using]
︙ | ︙ | |||
57 58 59 60 61 62 63 | #if defined(OF_ATOMIC_OPS) # import "atomic.h" #elif defined(OF_THREADS) # import "threading.h" #endif struct pre_ivar { | | | < > > > > > > > > > | 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 94 95 | #if defined(OF_ATOMIC_OPS) # import "atomic.h" #elif defined(OF_THREADS) # import "threading.h" #endif struct pre_ivar { int32_t retainCount; struct pre_mem *firstMem, *lastMem; #if !defined(OF_ATOMIC_OPS) && defined(OF_THREADS) of_spinlock_t retainCountSpinlock; #endif }; struct pre_mem { id owner; struct pre_mem *prev, *next; }; /* Hopefully no arch needs more than 16 bytes padding */ #ifndef __BIGGEST_ALIGNMENT__ # define __BIGGEST_ALIGNMENT__ 16 #endif #define PRE_IVAR_ALIGN ((sizeof(struct pre_ivar) + \ (__BIGGEST_ALIGNMENT__ - 1)) & ~(__BIGGEST_ALIGNMENT__ - 1)) #define PRE_IVAR ((struct pre_ivar*)(void*)((char*)self - PRE_IVAR_ALIGN)) #define PRE_MEM_ALIGN ((sizeof(struct pre_mem) + \ (__BIGGEST_ALIGNMENT__ - 1)) & ~(__BIGGEST_ALIGNMENT__ - 1)) #define PRE_MEM(mem) ((struct pre_mem*)(void*)((char*)mem - PRE_MEM_ALIGN)) #ifdef OF_OLD_GNU_RUNTIME extern void __objc_update_dispatch_table_for_class(Class); #endif static struct { Class isa; } alloc_failed_exception; |
︙ | ︙ | |||
234 235 236 237 238 239 240 | size_t instanceSize = class_getInstanceSize(self); if ((instance = calloc(instanceSize + PRE_IVAR_ALIGN, 1)) == NULL) { alloc_failed_exception.isa = [OFAllocFailedException class]; @throw (OFAllocFailedException*)&alloc_failed_exception; } | < < | 242 243 244 245 246 247 248 249 250 251 252 253 254 255 | size_t instanceSize = class_getInstanceSize(self); if ((instance = calloc(instanceSize + PRE_IVAR_ALIGN, 1)) == NULL) { alloc_failed_exception.isa = [OFAllocFailedException class]; @throw (OFAllocFailedException*)&alloc_failed_exception; } ((struct pre_ivar*)instance)->retainCount = 1; #if !defined(OF_ATOMIC_OPS) && defined(OF_THREADS) if (!of_spinlock_new( &((struct pre_ivar*)instance)->retainCountSpinlock)) { free(instance); @throw [OFInitializationFailedException |
︙ | ︙ | |||
749 750 751 752 753 754 755 | - (OFString*)description { /* Classes containing data should reimplement this! */ return [OFString stringWithFormat: @"<%@: %p>", [self className], self]; } | < < < < < < < < < < < < < < < < < < < < < < | < | < < < < | < | > > | | < < < < | > > | < < | | > > | > > < < < | | < | > | | > > > | > > > > < < > | 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 | - (OFString*)description { /* Classes containing data should reimplement this! */ return [OFString stringWithFormat: @"<%@: %p>", [self className], self]; } - (void*)allocMemoryWithSize: (size_t)size { void *pointer; struct pre_mem *preMem; if (size > SIZE_MAX - PRE_IVAR_ALIGN) @throw [OFOutOfRangeException exceptionWithClass: isa]; if ((pointer = malloc(PRE_MEM_ALIGN + size)) == NULL) @throw [OFOutOfMemoryException exceptionWithClass: isa requestedSize: size]; preMem = pointer; preMem->owner = self; preMem->prev = PRE_IVAR->lastMem; preMem->next = NULL; if (PRE_IVAR->lastMem != NULL) PRE_IVAR->lastMem->next = preMem; PRE_IVAR->lastMem = preMem; return (char*)pointer + PRE_MEM_ALIGN; } - (void*)allocMemoryForNItems: (size_t)nItems ofSize: (size_t)size { if (nItems == 0 || size == 0) return NULL; if (nItems > SIZE_MAX / size) @throw [OFOutOfRangeException exceptionWithClass: isa]; return [self allocMemoryWithSize: nItems * size]; } - (void*)resizeMemory: (void*)pointer toSize: (size_t)size { void *new; struct pre_mem *preMem; if (pointer == NULL) return [self allocMemoryWithSize: size]; if (size == 0) { [self freeMemory: pointer]; return NULL; } if (PRE_MEM(pointer)->owner != self) @throw [OFMemoryNotPartOfObjectException exceptionWithClass: isa pointer: pointer]; if ((new = realloc(PRE_MEM(pointer), PRE_MEM_ALIGN + size)) == NULL) @throw [OFOutOfMemoryException exceptionWithClass: isa requestedSize: size]; preMem = new; if (preMem != PRE_MEM(pointer)) { if (preMem->prev != NULL) preMem->prev->next = preMem; if (preMem->next != NULL) preMem->next->prev = preMem; if (PRE_IVAR->firstMem == PRE_MEM(pointer)) PRE_IVAR->firstMem = preMem; if (PRE_IVAR->lastMem == PRE_MEM(pointer)) PRE_IVAR->lastMem = preMem; } return (char*)new + PRE_MEM_ALIGN; } - (void*)resizeMemory: (void*)pointer toNItems: (size_t)nItems ofSize: (size_t)size { if (pointer == NULL) |
︙ | ︙ | |||
870 871 872 873 874 875 876 | return [self resizeMemory: pointer toSize: nItems * size]; } - (void)freeMemory: (void*)pointer { | < < < < | | < < | | < < < < | < | < | < < | < | | | | | < < < < | < | < | < < < | | 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 | return [self resizeMemory: pointer toSize: nItems * size]; } - (void)freeMemory: (void*)pointer { if (pointer == NULL) return; if (PRE_MEM(pointer)->owner != self) @throw [OFMemoryNotPartOfObjectException exceptionWithClass: isa pointer: pointer]; if (PRE_MEM(pointer)->prev != NULL) PRE_MEM(pointer)->prev->next = PRE_MEM(pointer)->next; if (PRE_MEM(pointer)->next != NULL) PRE_MEM(pointer)->next->prev = PRE_MEM(pointer)->prev; if (PRE_IVAR->firstMem == PRE_MEM(pointer)) PRE_IVAR->firstMem = PRE_MEM(pointer)->next; if (PRE_IVAR->lastMem == PRE_MEM(pointer)) PRE_IVAR->lastMem = PRE_MEM(pointer)->prev; /* To detect double-free */ PRE_MEM(pointer)->owner = nil; free(PRE_MEM(pointer)); } - retain { #if defined(OF_ATOMIC_OPS) of_atomic_inc_32(&PRE_IVAR->retainCount); #elif defined(OF_THREADS) |
︙ | ︙ | |||
987 988 989 990 991 992 993 | return NO; } - (void)dealloc { Class class; void (*last)(id, SEL) = NULL; | | | | | | | > > | 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 | return NO; } - (void)dealloc { Class class; void (*last)(id, SEL) = NULL; struct pre_mem *iter; for (class = isa; class != Nil; class = class_getSuperclass(class)) { void (*destruct)(id, SEL); if ([class instancesRespondToSelector: cxx_destruct]) { if ((destruct = (void(*)(id, SEL))[class instanceMethodForSelector: cxx_destruct]) != last) destruct(self, cxx_destruct); last = destruct; } else break; } iter = PRE_IVAR->firstMem; while (iter != NULL) { struct pre_mem *next = iter->next; free(iter); iter = next; } free((char*)self - PRE_IVAR_ALIGN); } /* Required to use properties with the Apple runtime */ - copyWithZone: (void*)zone { |
︙ | ︙ | |||
1035 1036 1037 1038 1039 1040 1041 | return [(id)self mutableCopy]; } /* * Those are needed as the root class is the superclass of the root class's * metaclass and thus instance methods can be sent to class objects as well. */ | < < < < < < | 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 | return [(id)self mutableCopy]; } /* * Those are needed as the root class is the superclass of the root class's * metaclass and thus instance methods can be sent to class objects as well. */ + (void*)allocMemoryWithSize: (size_t)size { @throw [OFNotImplementedException exceptionWithClass: self selector: _cmd]; } + (void*)allocMemoryForNItems: (size_t)nItems |
︙ | ︙ |