<commit_before>//
//  memory_management.c
//  xfig
//
//  Created by <NAME> on 12/10/13.
//  Copyright (c) 2013 <NAME>. All rights reserved.
//

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <memory_management/memory_management.h>


#define _MEMORY_MANAGEMENT_CANARY_VALUE 0xCA11ACAB
#define _MEMORY_MANAGEMENT_CANARY_BAD_VALUE 0xDEADDEAD

#define _MEMORY_MANAGEMENT_CANARY_ATTRIBUTE_NAME canary
#define _MEMORY_MANAGEMENT_RETAIN_COUNT_ATTRIBUTE_NAME retainCount
#define _MEMORY_MANAGEMENT_DEALLOC_ATTRIBUTE_NAME dealloc

#define _MEMORY_MANAGEMENT_PROTOTYPE_INTERNAL _memory_management_prototype_internal
#define _MEMORY_MANAGEMENT_INTERNAL_TYPE struct _memory_management_attributes_internal
#define _MEMORY_MANAGEMENT_INTERNAL_CAST(o) (((_MEMORY_MANAGEMENT_INTERNAL_TYPE *)(o))-1)
#define _MEMORY_MANAGEMENT_DECLARE_INTERNAL_VARIABLE(var) _MEMORY_MANAGEMENT_INTERNAL_TYPE *var

#define _MEMORY_MANAGEMENT_INITIALIZE(o) ((*o) = _MEMORY_MANAGEMENT_PROTOTYPE_INTERNAL)

#define _MEMORY_MANAGEMENT_CANARY_ATTRIBUTE(o) (o)->_MEMORY_MANAGEMENT_CANARY_ATTRIBUTE_NAME
#define _MEMORY_MANAGEMENT_CHECK_ENABLED(o) (_MEMORY_MANAGEMENT_CANARY_ATTRIBUTE(o) == _MEMORY_MANAGEMENT_CANARY_VALUE)
#define _MEMORY_MANAGEMENT_IS_INVALIDATED(o) (_MEMORY_MANAGEMENT_CANARY_ATTRIBUTE(o) == _MEMORY_MANAGEMENT_CANARY_BAD_VALUE)
#define _MEMORY_MANAGEMENT_INVALIDATE(o) (_MEMORY_MANAGEMENT_CANARY_ATTRIBUTE(o) = _MEMORY_MANAGEMENT_CANARY_BAD_VALUE)

#define _MEMORY_MANAGEMENT_INVALID_RETAIN_COUNT (unsigned int)(-1U)

#define _MEMORY_MANAGEMENT_RETAIN_COUNT_ATTRIBUTE(o) (o)->_MEMORY_MANAGEMENT_RETAIN_COUNT_ATTRIBUTE_NAME
#define _MEMORY_MANAGEMENT_ATOMIC_RETAIN(o)  (__sync_fetch_and_add(&(_MEMORY_MANAGEMENT_RETAIN_COUNT_ATTRIBUTE(o)), 1))
#define _MEMORY_MANAGEMENT_ATOMIC_RELEASE(o) (__sync_sub_and_fetch(&(_MEMORY_MANAGEMENT_RETAIN_COUNT_ATTRIBUTE(o)), 1))

#define _MEMORY_MANAGEMENT_DEALLOC_ATTRIBUTE(o) (o)->_MEMORY_MANAGEMENT_DEALLOC_ATTRIBUTE_NAME
#define _MEMORY_MANAGEMENT_CALL_DEALLOC(o) if (NULL != _MEMORY_MANAGEMENT_DEALLOC_ATTRIBUTE(o)) _MEMORY_MANAGEMENT_DEALLOC_ATTRIBUTE(o)(o+1)

_MEMORY_MANAGEMENT_INTERNAL_TYPE {
	unsigned int _MEMORY_MANAGEMENT_RETAIN_COUNT_ATTRIBUTE_NAME;
	unsigned int _MEMORY_MANAGEMENT_CANARY_ATTRIBUTE_NAME;
	void (*_MEMORY_MANAGEMENT_DEALLOC_ATTRIBUTE_NAME)(void *);
};

_MEMORY_MANAGEMENT_INTERNAL_TYPE _MEMORY_MANAGEMENT_PROTOTYPE_INTERNAL = {
	1ULL,
	_MEMORY_MANAGEMENT_CANARY_VALUE,
	NULL
};

void *memory_management_retain(void *o) {
	if (NULL==o) return NULL;
	
	_MEMORY_MANAGEMENT_DECLARE_INTERNAL_VARIABLE(object) = _MEMORY_MANAGEMENT_INTERNAL_CAST(o);
	if (!_MEMORY_MANAGEMENT_CHECK_ENABLED(object) || _MEMORY_MANAGEMENT_IS_INVALIDATED(object)) {
		if (_MEMORY_MANAGEMENT_IS_INVALIDATED(object))
			assert(0 && "Called retain() with invalid pointer.");
		return o;
	}
	_MEMORY_MANAGEMENT_ATOMIC_RETAIN(object);
	return o;
}

void memory_management_release(void *o) {
	if (NULL==o) return;
	_MEMORY_MANAGEMENT_DECLARE_INTERNAL_VARIABLE(object) = _MEMORY_MANAGEMENT_INTERNAL_CAST(o);
	if (!_MEMORY_MANAGEMENT_CHECK_ENABLED(object) || _MEMORY_MANAGEMENT_IS_INVALIDATED(object)) {
		if (_MEMORY_MANAGEMENT_IS_INVALIDATED(object))
			assert(0 && "Called release() with invalided pointer.");
		return;
	}
	
	unsigned long long result = _MEMORY_MANAGEMENT_ATOMIC_RELEASE(object);
	assert(result != _MEMORY_MANAGEMENT_INVALID_RETAIN_COUNT && "Sent retain() to invalid pointer.");
	if ( result == 0) {
		_MEMORY_MANAGEMENT_CALL_DEALLOC(object);
		_MEMORY_MANAGEMENT_INVALIDATE(object);
		free(object);
		return;
	}
}

void memory_management_attributes_set_dealloc_function(void *o, void (*deallocf)(void *)) {
	if (NULL==o) return;
	_MEMORY_MANAGEMENT_DECLARE_INTERNAL_VARIABLE(object) = _MEMORY_MANAGEMENT_INTERNAL_CAST(o);
	
	if (!_MEMORY_MANAGEMENT_CHECK_ENABLED(object) || _MEMORY_MANAGEMENT_IS_INVALIDATED(object))
		return;
	
	_MEMORY_MANAGEMENT_DEALLOC_ATTRIBUTE(object) = deallocf;
}

unsigned int memory_management_get_retain_count(const void *o) {
	if (NULL==o) return _MEMORY_MANAGEMENT_INVALID_RETAIN_COUNT;
	_MEMORY_MANAGEMENT_DECLARE_INTERNAL_VARIABLE(object) = _MEMORY_MANAGEMENT_INTERNAL_CAST(o);
	
	if (!_MEMORY_MANAGEMENT_CHECK_ENABLED(object) || _MEMORY_MANAGEMENT_IS_INVALIDATED(object))
		return _MEMORY_MANAGEMENT_INVALID_RETAIN_COUNT;
	
	return _MEMORY_MANAGEMENT_RETAIN_COUNT_ATTRIBUTE(object);
}

void *memory_management_alloc(size_t size) {
	size_t totalSize = sizeof(_MEMORY_MANAGEMENT_INTERNAL_TYPE) + size;
	_MEMORY_MANAGEMENT_INTERNAL_TYPE *o = calloc(1,  totalSize);
	if (NULL==o) return NULL;
	_MEMORY_MANAGEMENT_INITIALIZE(o);
	return o+1;
}


#undef _MEMORY_MANAGEMENT_CANARY_VALUE
#undef _MEMORY_MANAGEMENT_CANARY_BAD_VALUE


#undef _MEMORY_MANAGEMENT_RETAIN_COUNT_ATTRIBUTE_NAME
#undef _MEMORY_MANAGEMENT_CANARY_ATTRIBUTE_NAME
#undef _MEMORY_MANAGEMENT_DEALLOC_NAME

#undef _MEMORY_MANAGEMENT_PROTOTYPE_INTERNAL
#undef _MEMORY_MANAGEMENT_INTERNAL_TYPE
#undef _MEMORY_MANAGEMENT_INTERNAL_CAST
#undef _MEMORY_MANAGEMENT_DECLARE_INTERNAL_VARIABLE

#undef _MEMORY_MANAGEMENT_CANARY_ATTRIBUTE
#undef _MEMORY_MANAGEMENT_CHECK_ENABLED
#undef _MEMORY_MANAGEMENT_IS_INVALIDATED
#undef _MEMORY_MANAGEMENT_INVALIDATE
<commit_msg>Update to use errno.<commit_after>//
//  memory_management.c
//  xfig
//
//  Created by <NAME> on 12/10/13.
//  Copyright (c) 2013 <NAME>. All rights reserved.
//

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <memory_management/memory_management.h>
#include <errno.h>


#define _MEMORY_MANAGEMENT_CANARY_VALUE 0xCA11ACAB
#define _MEMORY_MANAGEMENT_CANARY_BAD_VALUE 0xDEADDEAD

#define _MEMORY_MANAGEMENT_CANARY_ATTRIBUTE_NAME canary
#define _MEMORY_MANAGEMENT_RETAIN_COUNT_ATTRIBUTE_NAME retainCount
#define _MEMORY_MANAGEMENT_DEALLOC_ATTRIBUTE_NAME dealloc

#define _MEMORY_MANAGEMENT_PROTOTYPE_INTERNAL _memory_management_prototype_internal
#define _MEMORY_MANAGEMENT_INTERNAL_TYPE struct _memory_management_attributes_internal
#define _MEMORY_MANAGEMENT_INTERNAL_CAST(o) (((_MEMORY_MANAGEMENT_INTERNAL_TYPE *)(o))-1)
#define _MEMORY_MANAGEMENT_DECLARE_INTERNAL_VARIABLE(var) _MEMORY_MANAGEMENT_INTERNAL_TYPE *var

#define _MEMORY_MANAGEMENT_INITIALIZE(o) ((*o) = _MEMORY_MANAGEMENT_PROTOTYPE_INTERNAL)

#define _MEMORY_MANAGEMENT_CANARY_ATTRIBUTE(o) (o)->_MEMORY_MANAGEMENT_CANARY_ATTRIBUTE_NAME
#define _MEMORY_MANAGEMENT_CHECK_ENABLED(o) (_MEMORY_MANAGEMENT_CANARY_ATTRIBUTE(o) == _MEMORY_MANAGEMENT_CANARY_VALUE)
#define _MEMORY_MANAGEMENT_IS_INVALIDATED(o) (_MEMORY_MANAGEMENT_CANARY_ATTRIBUTE(o) == _MEMORY_MANAGEMENT_CANARY_BAD_VALUE)
#define _MEMORY_MANAGEMENT_INVALIDATE(o) (_MEMORY_MANAGEMENT_CANARY_ATTRIBUTE(o) = _MEMORY_MANAGEMENT_CANARY_BAD_VALUE)

#define _MEMORY_MANAGEMENT_INVALID_RETAIN_COUNT (unsigned int)(-1U)

#define _MEMORY_MANAGEMENT_RETAIN_COUNT_ATTRIBUTE(o) (o)->_MEMORY_MANAGEMENT_RETAIN_COUNT_ATTRIBUTE_NAME
#define _MEMORY_MANAGEMENT_ATOMIC_RETAIN(o)  (__sync_fetch_and_add(&(_MEMORY_MANAGEMENT_RETAIN_COUNT_ATTRIBUTE(o)), 1))
#define _MEMORY_MANAGEMENT_ATOMIC_RELEASE(o) (__sync_sub_and_fetch(&(_MEMORY_MANAGEMENT_RETAIN_COUNT_ATTRIBUTE(o)), 1))

#define _MEMORY_MANAGEMENT_DEALLOC_ATTRIBUTE(o) (o)->_MEMORY_MANAGEMENT_DEALLOC_ATTRIBUTE_NAME
#define _MEMORY_MANAGEMENT_CALL_DEALLOC(o) if (NULL != _MEMORY_MANAGEMENT_DEALLOC_ATTRIBUTE(o)) _MEMORY_MANAGEMENT_DEALLOC_ATTRIBUTE(o)(o+1)

_MEMORY_MANAGEMENT_INTERNAL_TYPE {
	unsigned int _MEMORY_MANAGEMENT_RETAIN_COUNT_ATTRIBUTE_NAME;
	unsigned int _MEMORY_MANAGEMENT_CANARY_ATTRIBUTE_NAME;
	void (*_MEMORY_MANAGEMENT_DEALLOC_ATTRIBUTE_NAME)(void *);
};

_MEMORY_MANAGEMENT_INTERNAL_TYPE _MEMORY_MANAGEMENT_PROTOTYPE_INTERNAL = {
	1ULL,
	_MEMORY_MANAGEMENT_CANARY_VALUE,
	NULL
};

void *memory_management_retain(void *o) {
	if (NULL==o) return NULL;
	
	_MEMORY_MANAGEMENT_DECLARE_INTERNAL_VARIABLE(object) = _MEMORY_MANAGEMENT_INTERNAL_CAST(o);
	if (!_MEMORY_MANAGEMENT_CHECK_ENABLED(object) || _MEMORY_MANAGEMENT_IS_INVALIDATED(object)) {
		if (_MEMORY_MANAGEMENT_IS_INVALIDATED(object)) {
			assert(0 && "Called retain() with invalid pointer.");
		}
		return errno = EFAULT, o;
	}
	_MEMORY_MANAGEMENT_ATOMIC_RETAIN(object);
	return o;
}

void memory_management_release(void *o) {
	if (NULL==o) return;
	_MEMORY_MANAGEMENT_DECLARE_INTERNAL_VARIABLE(object) = _MEMORY_MANAGEMENT_INTERNAL_CAST(o);
	if (!_MEMORY_MANAGEMENT_CHECK_ENABLED(object) || _MEMORY_MANAGEMENT_IS_INVALIDATED(object)) {
		if (_MEMORY_MANAGEMENT_IS_INVALIDATED(object)) {
			assert(0 && "Called release() with invalided pointer.");
		}
		errno = EFAULT;
		return;
	}
	
	unsigned long long result = _MEMORY_MANAGEMENT_ATOMIC_RELEASE(object);
	assert(result != _MEMORY_MANAGEMENT_INVALID_RETAIN_COUNT && "Sent retain() to invalid pointer.");
	if ( result == 0) {
		_MEMORY_MANAGEMENT_CALL_DEALLOC(object);
		_MEMORY_MANAGEMENT_INVALIDATE(object);
		free(object);
		return;
	}
}

void memory_management_attributes_set_dealloc_function(void *o, void (*deallocf)(void *)) {
	if (NULL==o) return;
	_MEMORY_MANAGEMENT_DECLARE_INTERNAL_VARIABLE(object) = _MEMORY_MANAGEMENT_INTERNAL_CAST(o);
	
	if (!_MEMORY_MANAGEMENT_CHECK_ENABLED(object) || _MEMORY_MANAGEMENT_IS_INVALIDATED(object)) {
		errno = EFAULT;
		return;
	}
	
	_MEMORY_MANAGEMENT_DEALLOC_ATTRIBUTE(object) = deallocf;
}

unsigned int memory_management_get_retain_count(const void *o) {
	if (NULL==o) return _MEMORY_MANAGEMENT_INVALID_RETAIN_COUNT;
	_MEMORY_MANAGEMENT_DECLARE_INTERNAL_VARIABLE(object) = _MEMORY_MANAGEMENT_INTERNAL_CAST(o);
	
	if (!_MEMORY_MANAGEMENT_CHECK_ENABLED(object) || _MEMORY_MANAGEMENT_IS_INVALIDATED(object)) {
		errno = EFAULT;
		return _MEMORY_MANAGEMENT_INVALID_RETAIN_COUNT;
	}
	
	return _MEMORY_MANAGEMENT_RETAIN_COUNT_ATTRIBUTE(object);
}

void *memory_management_alloc(size_t size) {
	size_t totalSize = sizeof(_MEMORY_MANAGEMENT_INTERNAL_TYPE) + size;
	_MEMORY_MANAGEMENT_INTERNAL_TYPE *o = calloc(1,  totalSize);
	if (NULL==o) return errno = ENOMEM, (void *)NULL;
	_MEMORY_MANAGEMENT_INITIALIZE(o);
	return o+1;
}


#undef _MEMORY_MANAGEMENT_CANARY_VALUE
#undef _MEMORY_MANAGEMENT_CANARY_BAD_VALUE


#undef _MEMORY_MANAGEMENT_RETAIN_COUNT_ATTRIBUTE_NAME
#undef _MEMORY_MANAGEMENT_CANARY_ATTRIBUTE_NAME
#undef _MEMORY_MANAGEMENT_DEALLOC_NAME

#undef _MEMORY_MANAGEMENT_PROTOTYPE_INTERNAL
#undef _MEMORY_MANAGEMENT_INTERNAL_TYPE
#undef _MEMORY_MANAGEMENT_INTERNAL_CAST
#undef _MEMORY_MANAGEMENT_DECLARE_INTERNAL_VARIABLE

#undef _MEMORY_MANAGEMENT_CANARY_ATTRIBUTE
#undef _MEMORY_MANAGEMENT_CHECK_ENABLED
#undef _MEMORY_MANAGEMENT_IS_INVALIDATED
#undef _MEMORY_MANAGEMENT_INVALIDATE

{'fraction_non_alphanumeric': 0.08180615907802759, 'fraction_numerical': 0.004723219346306443, 'mean_word_length': 10.43304535637149, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 24, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1516193', 'n_tokens_mistral': 4175, 'n_tokens_neox': 4263, 'n_words': 524}