Comment: | Greatly improve OFObject's memory handling and performance.
A linked-list is put before each memory chunk allocated instead of As allocating bigger chunks seems to be a little bit slower than smaller |
---|---|
Downloads: | Tarball | ZIP archive | SQL archive |
Timelines: | family | ancestors | descendants | both | trunk |
Files: | files | file ages | folders |
SHA3-256: |
28170f5f658db22d78a3a3c69421419c |
User & Date: | js on 2012-03-15 11:29:24 |
Other Links: | manifest | tags |
2012-03-16
| ||
14:01 | One malloc less when allocating a string. check-in: 3005748a9d user: js tags: trunk | |
2012-03-15
| ||
11:29 | Greatly improve OFObject's memory handling and performance. check-in: 28170f5f65 user: js tags: trunk | |
2012-03-14
| ||
15:47 | gcc is stupid... check-in: 8cf554bd32 user: js tags: trunk | |
Modified src/OFBlock.m from [aca779a840] to [5825d5d284].
︙ | ︙ | |||
390 391 392 393 394 395 396 | { @throw [OFNotImplementedException exceptionWithClass: self selector: _cmd]; } - init { | < < < < < < | 390 391 392 393 394 395 396 397 398 399 400 401 402 403 | { @throw [OFNotImplementedException exceptionWithClass: self selector: _cmd]; } - init { @throw [OFNotImplementedException exceptionWithClass: isa selector: _cmd]; } - (void*)allocMemoryWithSize: (size_t)size { @throw [OFNotImplementedException exceptionWithClass: isa |
︙ | ︙ |
Modified src/OFConstantString.m from [1513456645] to [e6c815e41d].
︙ | ︙ | |||
51 52 53 54 55 56 57 | @implementation OFString_const + alloc { @throw [OFNotImplementedException exceptionWithClass: self selector: _cmd]; } | < < < < < | 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | @implementation OFString_const + alloc { @throw [OFNotImplementedException exceptionWithClass: self selector: _cmd]; } - (void*)allocMemoryWithSize: (size_t)size { @throw [OFNotImplementedException exceptionWithClass: isa selector: _cmd]; } |
︙ | ︙ | |||
177 178 179 180 181 182 183 | + alloc { @throw [OFNotImplementedException exceptionWithClass: self selector: _cmd]; } | < < < < < < | 172 173 174 175 176 177 178 179 180 181 182 183 184 185 | + alloc { @throw [OFNotImplementedException exceptionWithClass: self selector: _cmd]; } - (void*)allocMemoryWithSize: (size_t)size { @throw [OFNotImplementedException exceptionWithClass: isa selector: _cmd]; } - (void*)allocMemoryForNItems: (size_t)nitems |
︙ | ︙ |
Modified src/OFObject.h from [df231ae667] to [5a4773192e].
︙ | ︙ | |||
472 473 474 475 476 477 478 | * * This is mostly for debugging purposes. * * \return A description for the object */ - (OFString*)description; | < < < < < < < < < < | 472 473 474 475 476 477 478 479 480 481 482 483 484 485 | * * This is mostly for debugging purposes. * * \return A description for the object */ - (OFString*)description; /** * \brief Allocates memory and stores it in the object's memory pool. * * It will be free'd automatically when the object is deallocated. * * \param size The size of the memory to allocate * \return A pointer to the allocated memory |
︙ | ︙ |
Modified src/OFObject.m from [eb1f0d3652] to [329e3b32bd].
︙ | ︙ | |||
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 |
︙ | ︙ |
Modified src/OFString_UTF8.m from [df8f106867] to [2df9fbd0df].
︙ | ︙ | |||
431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 | - initWithFormat: (OFConstantString*)format arguments: (va_list)arguments { self = [super init]; @try { int cStringLength; if (format == nil) @throw [OFInvalidArgumentException exceptionWithClass: isa selector: _cmd]; s = [self allocMemoryWithSize: sizeof(*s)]; memset(s, 0, sizeof(*s)); | > | | | | | > > | | < | 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 | - initWithFormat: (OFConstantString*)format arguments: (va_list)arguments { self = [super init]; @try { char *tmp; int cStringLength; if (format == nil) @throw [OFInvalidArgumentException exceptionWithClass: isa selector: _cmd]; s = [self allocMemoryWithSize: sizeof(*s)]; memset(s, 0, sizeof(*s)); if ((cStringLength = of_vasprintf(&tmp, [format UTF8String], arguments)) == -1) @throw [OFInvalidFormatException exceptionWithClass: isa]; s->cStringLength = cStringLength; @try { switch (of_string_check_utf8(tmp, cStringLength, &s->length)) { case 1: s->UTF8 = YES; break; case -1: @throw [OFInvalidEncodingException exceptionWithClass: isa]; } s->cString = [self allocMemoryWithSize: cStringLength + 1]; memcpy(s->cString, tmp, cStringLength + 1); } @finally { free(tmp); } } @catch (id e) { [self release]; @throw e; } return self; |
︙ | ︙ |
Modified tests/OFObjectTests.m from [cd898a5b93] to [4509cfc31c].
︙ | ︙ | |||
36 37 38 39 40 41 42 | - (void)objectTests { OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init]; OFObject *obj = [[[OFObject alloc] init] autorelease]; void *p, *q, *r; OFObject *o; MyObj *m; | | < < < < < > > > > | | | | > | 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 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 | - (void)objectTests { OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init]; OFObject *obj = [[[OFObject alloc] init] autorelease]; void *p, *q, *r; OFObject *o; MyObj *m; char *tmp; TEST(@"Allocating 4096 bytes", (p = [obj allocMemoryWithSize: 4096]) != NULL) TEST(@"Freeing memory", R([obj freeMemory: p])) TEST(@"Allocating and freeing 4096 bytes 3 times", (p = [obj allocMemoryWithSize: 4096]) != NULL && (q = [obj allocMemoryWithSize: 4096]) != NULL && (r = [obj allocMemoryWithSize: 4096]) != NULL && R([obj freeMemory: p]) && R([obj freeMemory: q]) && R([obj freeMemory: r])) tmp = [self allocMemoryWithSize: 1024]; EXPECT_EXCEPTION(@"Detect freeing of memory not allocated by object", OFMemoryNotPartOfObjectException, [obj freeMemory: tmp]) EXPECT_EXCEPTION(@"Detect out of memory on alloc", OFOutOfMemoryException, [obj allocMemoryWithSize: SIZE_MAX - 128]) EXPECT_EXCEPTION(@"Detect out of memory on resize", OFOutOfMemoryException, { p = [obj allocMemoryWithSize: 1]; [obj resizeMemory: p toSize: SIZE_MAX - 128]; }) [obj freeMemory: p]; TEST(@"Allocate when trying to resize NULL", (p = [obj resizeMemory: NULL toSize: 1024]) != NULL) [obj freeMemory: p]; EXPECT_EXCEPTION(@"Detect resizing of memory not allocated by object", OFMemoryNotPartOfObjectException, [obj resizeMemory: tmp toSize: 2048]) [self freeMemory: tmp]; TEST(@"+[description]", [[OFObject description] isEqual: @"OFObject"] && [[MyObj description] isEqual: @"MyObj"]) o = [[[OFObject alloc] init] autorelease]; m = [[[MyObj alloc] init] autorelease]; |
︙ | ︙ |