ObjFW  Diff

Differences From Artifact [eb1f0d3652]:

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
64
65


66
67
68
69
70





71
72
73
74
75
76
77
78
79
80




81
82
83
84
85
86
87
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;
	void	      **memoryChunks;
	int32_t retainCount;
	struct pre_mem *firstMem, *lastMem;
	unsigned int  memoryChunksSize;
#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
241
242
243
244
245
246
247
248
249
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)->memoryChunks = NULL;
	((struct pre_ivar*)instance)->memoryChunksSize = 0;
	((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
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


843
844


845
846
847
848










849
850
851
852

853
854
855
856
857
858
859
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)addMemoryToPool: (void*)pointer
{
	void **memoryChunks;
	unsigned int memoryChunksSize;

	memoryChunksSize = PRE_IVAR->memoryChunksSize + 1;

	if (UINT_MAX - PRE_IVAR->memoryChunksSize < 1 ||
	    memoryChunksSize > UINT_MAX / sizeof(void*))
		@throw [OFOutOfRangeException exceptionWithClass: isa];

	if ((memoryChunks = realloc(PRE_IVAR->memoryChunks,
	    memoryChunksSize * sizeof(void*))) == NULL)
		@throw [OFOutOfMemoryException
		    exceptionWithClass: isa
			 requestedSize: memoryChunksSize];

	PRE_IVAR->memoryChunks = memoryChunks;
	PRE_IVAR->memoryChunks[PRE_IVAR->memoryChunksSize] = pointer;
	PRE_IVAR->memoryChunksSize = memoryChunksSize;
}

- (void*)allocMemoryWithSize: (size_t)size
{
	void *pointer, **memoryChunks;
	void *pointer;
	unsigned int memoryChunksSize;

	struct pre_mem *preMem;
	if (size == 0)
		return NULL;

	memoryChunksSize = PRE_IVAR->memoryChunksSize + 1;

	if (UINT_MAX - PRE_IVAR->memoryChunksSize == 0 ||
	if (size > SIZE_MAX - PRE_IVAR_ALIGN)
	    memoryChunksSize > UINT_MAX / sizeof(void*))
		@throw [OFOutOfRangeException exceptionWithClass: isa];

	if ((pointer = malloc(size)) == NULL)
	if ((pointer = malloc(PRE_MEM_ALIGN + size)) == NULL)
		@throw [OFOutOfMemoryException exceptionWithClass: isa
						    requestedSize: size];
	preMem = pointer;

	preMem->owner = self;
	if ((memoryChunks = realloc(PRE_IVAR->memoryChunks,
	    memoryChunksSize * sizeof(void*))) == NULL) {
	preMem->prev = PRE_IVAR->lastMem;
	preMem->next = NULL;
		free(pointer);
		@throw [OFOutOfMemoryException
		    exceptionWithClass: isa
			 requestedSize: memoryChunksSize];
	}

	if (PRE_IVAR->lastMem != NULL)
		PRE_IVAR->lastMem->next = preMem;

	PRE_IVAR->memoryChunks = memoryChunks;
	PRE_IVAR->lastMem = preMem;
	PRE_IVAR->memoryChunks[PRE_IVAR->memoryChunksSize] = pointer;
	PRE_IVAR->memoryChunksSize = memoryChunksSize;

	return pointer;
	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 **iter;
	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)
	iter = PRE_IVAR->memoryChunks + PRE_IVAR->memoryChunksSize;
		@throw [OFMemoryNotPartOfObjectException
		    exceptionWithClass: isa
			       pointer: pointer];

	while (iter-- > PRE_IVAR->memoryChunks) {
		if (OF_UNLIKELY(*iter == pointer)) {
			if (OF_UNLIKELY((pointer = realloc(pointer,
			    size)) == NULL))
				@throw [OFOutOfMemoryException
	if ((new = realloc(PRE_MEM(pointer), PRE_MEM_ALIGN + size)) == NULL)
		@throw [OFOutOfMemoryException exceptionWithClass: isa
				     exceptionWithClass: isa
					  requestedSize: size];
						    requestedSize: size];
	preMem = new;

			*iter = pointer;
			return pointer;
		}
	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;
	}

	@throw [OFMemoryNotPartOfObjectException exceptionWithClass: isa
							    pointer: pointer];
	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
877
878
879
880
881
882
883
884
885


886
887
888
889


890
891
892
893
894
895

896
897

898
899

900
901
902

903
904
905
906
907
908





909
910
911
912
913
914

915
916

917
918

919
920
921
922

923
924
925
926
927
928
929
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
{
	void **iter, *last, **memoryChunks;
	unsigned int i, memoryChunksSize;

	if (pointer == NULL)
		return;

	iter = PRE_IVAR->memoryChunks + PRE_IVAR->memoryChunksSize;
	i = PRE_IVAR->memoryChunksSize;

	if (PRE_MEM(pointer)->owner != self)
		@throw [OFMemoryNotPartOfObjectException
	while (iter-- > PRE_IVAR->memoryChunks) {
		i--;

		if (OF_UNLIKELY(*iter == pointer)) {
		    exceptionWithClass: isa
			       pointer: pointer];
			memoryChunksSize = PRE_IVAR->memoryChunksSize - 1;
			last = PRE_IVAR->memoryChunks[memoryChunksSize];

			assert(PRE_IVAR->memoryChunksSize != 0 &&
			    memoryChunksSize <= UINT_MAX / sizeof(void*));

	if (PRE_MEM(pointer)->prev != NULL)
			if (OF_UNLIKELY(memoryChunksSize == 0)) {
				free(pointer);
		PRE_MEM(pointer)->prev->next = PRE_MEM(pointer)->next;
				free(PRE_IVAR->memoryChunks);

	if (PRE_MEM(pointer)->next != NULL)
				PRE_IVAR->memoryChunks = NULL;
				PRE_IVAR->memoryChunksSize = 0;

		PRE_MEM(pointer)->next->prev = PRE_MEM(pointer)->prev;
				return;
			}

			free(pointer);
			PRE_IVAR->memoryChunks[i] = last;
			PRE_IVAR->memoryChunksSize = memoryChunksSize;

	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;

			if (OF_UNLIKELY((memoryChunks = realloc(
			    PRE_IVAR->memoryChunks, memoryChunksSize *
			    sizeof(void*))) == NULL))
				return;

	/* To detect double-free */
			PRE_IVAR->memoryChunks = memoryChunks;

	PRE_MEM(pointer)->owner = nil;
			return;
		}

	}

	@throw [OFMemoryNotPartOfObjectException exceptionWithClass: isa
							    pointer: pointer];
	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
994

995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011



1012
1013
1014




1015
1016
1017
1018
1019
1020
1021
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;
	void **iter;
	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->memoryChunks + PRE_IVAR->memoryChunksSize;
	while (iter-- > PRE_IVAR->memoryChunks)
		free(*iter);
	iter = PRE_IVAR->firstMem;
	while (iter != NULL) {
		struct pre_mem *next = iter->next;

	if (PRE_IVAR->memoryChunks != NULL)
		free(PRE_IVAR->memoryChunks);
		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
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
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)addMemoryToPool: (void*)pointer
{
	@throw [OFNotImplementedException exceptionWithClass: self
						    selector: _cmd];
}

+ (void*)allocMemoryWithSize: (size_t)size
{
	@throw [OFNotImplementedException exceptionWithClass: self
						    selector: _cmd];
}

+ (void*)allocMemoryForNItems: (size_t)nItems