src/Structures.cpp
changeset 147 9c544823d090
parent 146 dc7d3b7a918c
child 153 fc91c4a3dfe2
     1.1 --- a/src/Structures.cpp	Sat Aug 04 17:51:43 2012 -0700
     1.2 +++ b/src/Structures.cpp	Sat Aug 04 19:33:06 2012 -0700
     1.3 @@ -16,6 +16,7 @@
     1.4   * You should have received a copy of the GNU General Public License along with
     1.5   * SourcePawn. If not, see http://www.gnu.org/licenses/.
     1.6   */
     1.7 +#include <string.h>
     1.8  #include "Structures.h"
     1.9  #include "Zone.h"
    1.10  #include "Types.h"
    1.11 @@ -62,3 +63,35 @@
    1.12      return desc;
    1.13  }
    1.14  
    1.15 +StructMap *
    1.16 +StructMap::New(Zone *zone, const StructureBufferBuilder &builder)
    1.17 +{
    1.18 +    size_t bytesNeeded = sizeof(StructMap) +
    1.19 +                         (builder.traceableOffsetCount() * sizeof(unsigned));
    1.20 +
    1.21 +    Local<StructMap> map(zone);
    1.22 +    map = StructMap::cast(zone->allocateMap(MapKind_Struct, bytesNeeded, MapKind_MetaStruct));
    1.23 +    if (!map)
    1.24 +        return NULL;
    1.25 +
    1.26 +    map->structureSize_ = builder.structureSize();
    1.27 +    map->traceOffsetCount_ = builder.traceableOffsetCount();
    1.28 +    map->type_ = NULL;
    1.29 +    memcpy(const_cast<unsigned *>(map->traceOffsets()), builder.offsets(),
    1.30 +           sizeof(unsigned) * builder.traceableOffsetCount());
    1.31 +
    1.32 +    return map;
    1.33 +}
    1.34 +
    1.35 +Struct *
    1.36 +Struct::New(Zone *zone, Handle<StructMap> map, Heap::Tenure tenure)
    1.37 +{
    1.38 +    size_t bytesNeeded = sizeof(Struct) + map->structureSize();
    1.39 +    Local<Struct> obj(zone, Struct::cast(zone->allocate(map, bytesNeeded, tenure)));
    1.40 +    if (!obj)
    1.41 +        return NULL;
    1.42 +
    1.43 +    memset((Struct *)obj + 1, 0, map->structureSize());
    1.44 +    return obj;
    1.45 +}
    1.46 +