2012-04-24 01:22:36 +00:00
|
|
|
/*
|
2015-07-15 21:23:19 +00:00
|
|
|
Copyright 2015 Wolfgang Thaller.
|
2012-04-24 01:22:36 +00:00
|
|
|
|
2014-09-14 22:43:30 +00:00
|
|
|
This file is part of Retro68.
|
2012-04-24 01:22:36 +00:00
|
|
|
|
2014-09-14 22:43:30 +00:00
|
|
|
Retro68 is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
2012-04-24 01:22:36 +00:00
|
|
|
|
2014-09-14 22:43:30 +00:00
|
|
|
Retro68 is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
2012-04-24 01:22:36 +00:00
|
|
|
|
2015-07-15 21:23:19 +00:00
|
|
|
Under Section 7 of GPL version 3, you are granted additional
|
|
|
|
permissions described in the GCC Runtime Library Exception, version
|
|
|
|
3.1, as published by the Free Software Foundation.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License and
|
|
|
|
a copy of the GCC Runtime Library Exception along with this program;
|
|
|
|
see the files COPYING and COPYING.RUNTIME respectively. If not, see
|
|
|
|
<http://www.gnu.org/licenses/>.
|
2012-04-24 01:22:36 +00:00
|
|
|
*/
|
|
|
|
|
2012-03-29 08:29:41 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <reent.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <MacMemory.h>
|
|
|
|
|
|
|
|
void referenceMyMalloc() {}
|
|
|
|
|
2017-10-07 00:03:37 +00:00
|
|
|
int* __errno()
|
|
|
|
{
|
|
|
|
static int realErrno = 0;
|
|
|
|
return &realErrno;
|
|
|
|
}
|
|
|
|
|
2012-03-29 08:29:41 +00:00
|
|
|
void *_malloc_r(struct _reent *reent_ptr, size_t sz)
|
|
|
|
{
|
2014-09-30 08:50:56 +00:00
|
|
|
Ptr p = NewPtr(sz);
|
|
|
|
|
|
|
|
if(!p)
|
2017-10-08 17:28:10 +00:00
|
|
|
reent_ptr->_errno = ENOMEM;
|
2014-09-30 08:50:56 +00:00
|
|
|
|
|
|
|
return p;
|
2012-03-29 08:29:41 +00:00
|
|
|
}
|
|
|
|
void *_calloc_r(struct _reent *reent_ptr, size_t sz, size_t sz2)
|
|
|
|
{
|
2014-09-30 08:50:56 +00:00
|
|
|
Ptr p = NewPtrClear(sz*sz2);
|
|
|
|
|
|
|
|
if(!p)
|
2017-10-08 17:28:10 +00:00
|
|
|
reent_ptr->_errno = ENOMEM;
|
2014-09-30 08:50:56 +00:00
|
|
|
|
|
|
|
return p;
|
2012-03-29 08:29:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void _free_r(struct _reent *reent_ptr, void *ptr)
|
|
|
|
{
|
2014-09-14 22:43:30 +00:00
|
|
|
if(ptr != NULL)
|
|
|
|
DisposePtr(ptr);
|
2012-03-29 08:29:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void *_realloc_r(struct _reent *reent_ptr, void *ptr, size_t sz)
|
|
|
|
{
|
2014-09-14 22:43:30 +00:00
|
|
|
if(ptr == NULL)
|
|
|
|
{
|
2014-09-30 08:50:56 +00:00
|
|
|
Ptr p = NewPtr(sz);
|
|
|
|
|
|
|
|
if(!p)
|
2017-10-08 17:28:10 +00:00
|
|
|
reent_ptr->_errno = ENOMEM;
|
2014-09-30 08:50:56 +00:00
|
|
|
|
|
|
|
return p;
|
2014-09-14 22:43:30 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
MemError();
|
|
|
|
SetPtrSize(ptr, sz);
|
|
|
|
if(MemError())
|
|
|
|
{
|
|
|
|
size_t oldSz = GetPtrSize(ptr);
|
|
|
|
if(sz > oldSz)
|
|
|
|
{
|
|
|
|
void *newPtr = NewPtr(sz);
|
|
|
|
if(!newPtr)
|
|
|
|
{
|
2017-10-08 17:28:10 +00:00
|
|
|
reent_ptr->_errno = ENOMEM;
|
2014-09-14 22:43:30 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
memcpy(newPtr, ptr, oldSz);
|
|
|
|
return newPtr;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-10-08 17:28:10 +00:00
|
|
|
reent_ptr->_errno = ENOMEM;
|
2014-09-14 22:43:30 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return ptr;
|
|
|
|
}
|
2012-03-29 08:29:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void *malloc(size_t sz)
|
|
|
|
{
|
2014-09-14 22:43:30 +00:00
|
|
|
return _malloc_r(_REENT, sz);
|
2012-03-29 08:29:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void free(void *p)
|
|
|
|
{
|
2014-09-14 22:43:30 +00:00
|
|
|
_free_r(_REENT, p);
|
2012-03-29 08:29:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void *realloc(void *ptr, size_t sz)
|
|
|
|
{
|
2014-09-14 22:43:30 +00:00
|
|
|
return _realloc_r(_REENT, ptr, sz);
|
2012-03-29 08:29:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void *calloc(size_t sz1, size_t sz2)
|
|
|
|
{
|
2014-09-14 22:43:30 +00:00
|
|
|
return _calloc_r(_REENT, sz1, sz2);
|
2012-03-29 08:29:41 +00:00
|
|
|
}
|
2017-10-07 14:10:05 +00:00
|
|
|
|
|
|
|
void *memalign(size_t alignment, size_t sz)
|
|
|
|
{
|
|
|
|
Ptr p = NewPtr(sz);
|
|
|
|
|
|
|
|
if(!p)
|
|
|
|
errno = ENOMEM;
|
|
|
|
|
|
|
|
// FIXME:
|
|
|
|
// NewPtr aligns to 4 bytes on 68020 and 68030,
|
|
|
|
// and to 16 bytes on 68040 and PowerPC.
|
|
|
|
|
|
|
|
// Do something else when more alignment is required.
|
|
|
|
// This might be hard, as adding extra overhead to all normal allocations
|
|
|
|
// just so that we can distinguish things in free() doesn't sound like it's worth it.
|
|
|
|
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|