v1.1.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Modules Pages
filesystem.h
1 /* -*- mode: C; c-basic-offset: 4; intent-tabs-mode: nil -*-
2  *
3  * Sifteo SDK
4  *
5  * Copyright <c> 2012 Sifteo, Inc.
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a copy
8  * of this software and associated documentation files (the "Software"), to deal
9  * in the Software without restriction, including without limitation the rights
10  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11  * copies of the Software, and to permit persons to whom the Software is
12  * furnished to do so, subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included in
15  * all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23  * THE SOFTWARE.
24  */
25 
26 #pragma once
27 #ifdef NOT_USERSPACE
28 # error This is a userspace-only header, not allowed by the current build.
29 #endif
30 
31 #include <sifteo/array.h>
32 #include <sifteo/asset/loader.h>
33 #include <sifteo/abi.h>
34 
35 namespace Sifteo {
36 
75 class StoredObject {
76 public:
77  _SYSObjectKey sys;
78 
80  static const unsigned LIMIT = _SYS_FS_MAX_OBJECT_KEYS - 1;
81 
83  static const unsigned MAX_SIZE = _SYS_FS_MAX_OBJECT_SIZE;
84 
86  StoredObject(_SYSObjectKey k) : sys(k) {}
87 
89  StoredObject(int k) : sys(k) {
90  ASSERT(k >= 0 && k <= LIMIT);
91  }
92 
94  StoredObject(unsigned k) : sys(k) {
95  ASSERT(k <= LIMIT);
96  }
97 
99  operator const _SYSObjectKey () const { return sys; }
100 
113  return StoredObject(LIMIT - _SYS_lti_counter("Sifteo.StoredObject", 0));
114  }
115 
145  int read(void *buffer, unsigned bufferSize, _SYSVolumeHandle volume = 0) const {
146  return _SYS_fs_objectRead(sys, (uint8_t*)buffer, bufferSize, volume);
147  }
148 
172  int write(const void *data, unsigned dataSize) const {
173  return _SYS_fs_objectWrite(sys, (const uint8_t*)data, dataSize);
174  }
175 
177  int erase() const {
178  return write(0, 0);
179  }
180 
187  template <typename T>
188  int read(T &buffer, _SYSVolumeHandle volume = 0) const {
189  return read((void*) &buffer, sizeof buffer, volume);
190  }
191 
193  template <typename T>
194  int readObject(T &buffer, _SYSVolumeHandle volume = 0) const {
195  return read((void*) &buffer, sizeof buffer, volume);
196  }
197 
204  template <typename T>
205  int write(const T &buffer) const {
206  return write((const void*) &buffer, sizeof buffer);
207  }
208 
210  template <typename T>
211  int writeObject(const T &buffer) const {
212  return write((const void*) &buffer, sizeof buffer);
213  }
214 
216  bool operator== (_SYSObjectKey other) const {
217  return sys == other;
218  }
219 
221  bool operator!= (_SYSObjectKey other) const {
222  return sys != other;
223  }
224 };
225 
226 
238 class Volume {
239 public:
240  _SYSVolumeHandle sys;
241 
243  enum Type {
244  T_GAME = _SYS_FS_VOL_GAME,
245  T_LAUNCHER = _SYS_FS_VOL_LAUNCHER,
246  };
247 
249  Volume() : sys(0) {}
250 
252  Volume(_SYSVolumeHandle vh) : sys(vh) {}
253 
255  operator const _SYSVolumeHandle () const { return sys; }
256 
262  template <unsigned T>
263  static void list(unsigned volType, Array<Volume, T> &volumes)
264  {
265  volumes.setCount(_SYS_fs_listVolumes(volType, &volumes.begin()->sys,
266  volumes.capacity()));
267  }
268 
289  void exec() const {
290  _SYS_elf_exec(sys);
291  }
292 
300  static Volume running() {
301  return (_SYSVolumeHandle) _SYS_fs_runningVolume();
302  }
303 
315  static Volume previous() {
316  return (_SYSVolumeHandle) _SYS_fs_previousVolume();
317  }
318 
320  bool operator== (_SYSVolumeHandle other) const {
321  return sys == other;
322  }
323 
325  bool operator!= (_SYSVolumeHandle other) const {
326  return sys != other;
327  }
328 };
329 
330 
349  Volume vol;
350  uint32_t offset;
351 
352  /*
353  * On debug builds, this ensures that only one MappedVolume exists
354  * at a time. On release builds, this has no effect and the static
355  * variable optimizes out.
356  */
357  void debugInstanceCounter(int delta) {
358  DEBUG_ONLY({
359  static int counter = 0;
360  counter += delta;
361  ASSERT(counter >= 0);
362  ASSERT(counter <= 1);
363  });
364  }
365 
366 public:
367  typedef _SYSUUID UUID;
368  typedef _SYSMetadataCubeRange CubeRange;
369 
370  static const unsigned MAX_BOOTSTRAP_GROUPS = _SYS_MAX_METADATA_ITEM_BYTES / sizeof(_SYSMetadataBootAsset);
371 
374 
377 
384  : vol(0), offset(0) {
385  debugInstanceCounter(1);
386  }
387 
394  explicit MappedVolume(Volume vol) {
395  debugInstanceCounter(1);
396  attach(vol);
397  }
398 
399  ~MappedVolume()
400  {
401  debugInstanceCounter(-1);
402 
403  // Reclaim a little bit of memory by unmapping
404  _SYS_elf_map(0);
405  }
406 
413  void attach(Volume vol)
414  {
415  if (this->vol != vol) {
416  this->vol = vol;
417  offset = _SYS_elf_map(vol);
418  }
419  }
420 
422  Volume volume() const {
423  return vol;
424  }
425 
440  void *metadata(unsigned key, unsigned minSize, unsigned *actualSize) const {
441  return _SYS_elf_metadata(vol, key, minSize, actualSize);
442  }
443 
459  template <typename T>
460  const T* metadata(unsigned key, unsigned *actualSize = NULL) const {
461  return reinterpret_cast<const T*>(metadata(key, sizeof(T), actualSize));
462  }
463 
472  template <typename T>
473  T* translate(T* va) const {
474  return reinterpret_cast<T*>(translate(reinterpret_cast<uint32_t>(va)));
475  }
476 
482  uint32_t translate(uint32_t va) const {
483  return va + offset;
484  }
485 
492  const char *title(const char *placeholder = "(untitled)") const {
493  const char *p = metadata<char>(_SYS_METADATA_TITLE_STR);
494  return p ? p : placeholder;
495  }
496 
503  const char *package(const char *placeholder = "(none)") const {
504  const char *p = metadata<char>(_SYS_METADATA_PACKAGE_STR);
505  return p ? p : placeholder;
506  }
507 
514  const char *version(const char *placeholder = "(none)") const {
515  const char *p = metadata<char>(_SYS_METADATA_VERSION_STR);
516  return p ? p : placeholder;
517  }
518 
524  const UUID *uuid() const {
525  const UUID *p = metadata<UUID>(_SYS_METADATA_UUID);
526  static UUID zero;
527  return p ? p : &zero;
528  }
529 
540  void translate(const _SYSMetadataBootAsset &meta, AssetGroup &group)
541  {
542  bzero(group);
543  group.sys.pHdr = translate(meta.pHdr);
544  }
545 
563  void translate(const _SYSMetadataImage *meta, AssetImage &image, AssetGroup &group)
564  {
565  bzero(group);
566  group.sys.pHdr = translate(meta->groupHdr);
567 
568  bzero(image);
569  image.sys.pAssetGroup = reinterpret_cast<uint32_t>(&group);
570  image.sys.width = meta->width;
571  image.sys.height = meta->height;
572  image.sys.frames = meta->frames;
573  image.sys.format = meta->format;
574  image.sys.pData = translate(meta->pData);
575  }
576 
581  void getBootstrap(BootstrapAssetGroups &groups, BootstrapAssetConfiguration &config)
582  {
583  uint32_t actual;
584  auto vec = metadata<_SYSMetadataBootAsset>(_SYS_METADATA_BOOT_ASSET, &actual);
585  if (!vec)
586  return;
587 
588  unsigned count = actual / sizeof *vec;
589  for (unsigned i = 0; i != count; ++i) {
590 
591  AssetGroup &group = groups.append();
592  translate(vec[i], group);
593 
594  config.append(vec[i].slot, group, volume());
595  }
596  }
597 };
598 
599 
612 {
613 public:
614  _SYSFilesystemInfo sys;
615 
622  void gather() {
623  _SYS_fs_info(&sys, sizeof sys);
624  }
625 
632  uint32_t allocationUnitSize() {
633  return sys.unitSize;
634  }
635 
637  uint32_t freeUnits() {
638  return sys.freeUnits;
639  }
640 
642  uint32_t freeBytes() {
643  return sys.freeUnits * sys.unitSize;
644  }
645 
647  uint32_t totalUnits() {
648  return sys.totalUnits;
649  }
650 
652  uint32_t totalBytes() {
653  return sys.totalUnits * sys.unitSize;
654  }
655 
663  uint32_t systemUnits() {
664  return sys.systemUnits;
665  }
666 
668  uint32_t systemBytes() {
669  return sys.systemUnits * sys.unitSize;
670  }
671 
673  uint32_t launcherElfUnits() {
674  return sys.launcherElfUnits;
675  }
676 
678  uint32_t launcherElfBytes() {
679  return sys.launcherElfUnits * sys.unitSize;
680  }
681 
683  uint32_t launcherObjUnits() {
684  return sys.launcherObjUnits;
685  }
686 
688  uint32_t launcherObjBytes() {
689  return sys.launcherObjUnits * sys.unitSize;
690  }
691 
693  uint32_t gameElfUnits() {
694  return sys.gameElfUnits;
695  }
696 
698  uint32_t gameElfBytes() {
699  return sys.gameElfUnits * sys.unitSize;
700  }
701 
703  uint32_t gameObjUnits() {
704  return sys.gameObjUnits;
705  }
706 
708  uint32_t gameObjBytes() {
709  return sys.gameObjUnits * sys.unitSize;
710  }
711 
713  uint32_t selfElfUnits() {
714  return sys.selfElfUnits;
715  }
716 
718  uint32_t selfElfBytes() {
719  return sys.selfElfUnits * sys.unitSize;
720  }
721 
723  uint32_t selfObjUnits() {
724  return sys.selfObjUnits;
725  }
726 
728  uint32_t selfObjBytes() {
729  return sys.selfObjUnits * sys.unitSize;
730  }
731 };
732 
733 
738 } // namespace Sifteo
void getBootstrap(BootstrapAssetGroups &groups, BootstrapAssetConfiguration &config)
Get the bootstrap assets for this Volume, returning the result in the provided BootstrapAssetConfigur...
Definition: filesystem.h:581
static unsigned capacity()
Retrieve the capacity of this array, always constant at compile-time.
Definition: array.h:64
static Volume previous()
Return the Volume corresponding to the previously running program.
Definition: filesystem.h:315
StoredObject(_SYSObjectKey k)
Initialize from a system object.
Definition: filesystem.h:86
uint32_t totalUnits()
Total space, in allocation units.
Definition: filesystem.h:647
uint32_t gameElfUnits()
Space used by all game ELF binaries, in allocation units.
Definition: filesystem.h:693
static Volume running()
Return the Volume corresponding to the currently running program.
Definition: filesystem.h:300
T & append(const T &newItem)
Synonym for push_back()
Definition: array.h:115
void translate(const _SYSMetadataBootAsset &meta, AssetGroup &group)
Translate a bootstrap asset metadata record to an AssetGroup.
Definition: filesystem.h:540
A bundle of compressed tile data, for use by AssetImages.
Definition: group.h:54
A Volume that has been mapped into the secondary flash memory region.
Definition: filesystem.h:348
void translate(const _SYSMetadataImage *meta, AssetImage &image, AssetGroup &group)
Translate an image metadata record to an AssetImage.
Definition: filesystem.h:563
uint32_t allocationUnitSize()
Return the size of one allocation unit.
Definition: filesystem.h:632
#define ASSERT(_x)
Runtime debug assertion.
Definition: macros.h:205
int read(T &buffer, _SYSVolumeHandle volume=0) const
Template wrapper for read() of fixed-size objects.
Definition: filesystem.h:188
Volume(_SYSVolumeHandle vh)
Initialize from a system object.
Definition: filesystem.h:252
uint32_t freeUnits()
Free space, in allocation units.
Definition: filesystem.h:637
bool operator!=(_SYSVolumeHandle other) const
Inequality comparison operator.
Definition: filesystem.h:325
uint32_t selfObjUnits()
Space used by the current volume's StoredObjects, in allocation units.
Definition: filesystem.h:723
A coarse-grained region of external memory.
Definition: filesystem.h:238
An AssetConfiguration represents an arrangement of AssetGroups to load.
Definition: loader.h:138
const T * metadata(unsigned key, unsigned *actualSize=NULL) const
Look up a metadata value from the mapped Volume. The result is cast to a constant pointer of the indi...
Definition: filesystem.h:460
T * translate(T *va) const
Translate a virtual address in this ELF volume's read-only data segment, into a virtual address in th...
Definition: filesystem.h:473
bool operator==(_SYSObjectKey other) const
Equality comparison operator.
Definition: filesystem.h:216
int erase() const
Erase this object. Equivalent to a zero-length write()
Definition: filesystem.h:177
uint32_t selfElfBytes()
Space used by the current volume's ELF binary, in bytes.
Definition: filesystem.h:718
void bzero(void *s, unsigned count)
Write 'n' zero bytes to memory.
Definition: memory.h:114
int read(void *buffer, unsigned bufferSize, _SYSVolumeHandle volume=0) const
Read a stored object into memory.
Definition: filesystem.h:145
int writeObject(const T &buffer) const
Template wrapper for write() of fixed-size objects.
Definition: filesystem.h:211
MappedVolume()
Construct a detached MappedVolume.
Definition: filesystem.h:383
uint32_t launcherObjUnits()
Space used by the launcher's StoredObjects, in allocation units.
Definition: filesystem.h:683
static StoredObject allocate()
Allocate a unique StoredObject value at compile-time.
Definition: filesystem.h:112
uint32_t freeBytes()
Free space, in bytes.
Definition: filesystem.h:642
uint32_t selfObjBytes()
Space used by the current volume's StoredObjects, in bytes.
Definition: filesystem.h:728
const UUID * uuid() const
Retrieve the volume's UUID.
Definition: filesystem.h:524
AssetConfiguration< MAX_BOOTSTRAP_GROUPS > BootstrapAssetConfiguration
An AssetConfiguration large enough to hold all bootstrap groups.
Definition: filesystem.h:373
int readObject(T &buffer, _SYSVolumeHandle volume=0) const
Template wrapper for read() of fixed-size objects.
Definition: filesystem.h:194
int write(const T &buffer) const
Template wrapper for write() of fixed-size objects.
Definition: filesystem.h:205
MappedVolume(Volume vol)
Map a view of the provided Volume.
Definition: filesystem.h:394
static void list(unsigned volType, Array< Volume, T > &volumes)
List all volumes of the specified type.
Definition: filesystem.h:263
void setCount(unsigned c)
Change the number of elements currently in the array.
Definition: array.h:80
void gather()
Inspects the current state of the filesystem, and fills in the FilesystemInfo accordingly.
Definition: filesystem.h:622
iterator begin()
Return an iterator pointing to the first slot in the array.
Definition: array.h:182
const char * title(const char *placeholder="(untitled)") const
Retrieve a mapped, NUL-terminated string with the volume's title.
Definition: filesystem.h:492
const char * package(const char *placeholder="(none)") const
Retrieve a mapped, NUL-terminated string with the volume's package string.
Definition: filesystem.h:503
Any kind of asset image, as defined in your stir script.
Definition: image.h:67
#define DEBUG_ONLY(_x)
Mark a chunk of code as debug-only.
Definition: macros.h:117
uint32_t systemUnits()
Definition: filesystem.h:663
uint32_t launcherElfBytes()
Space used by the launcher's ELF binary, in bytes.
Definition: filesystem.h:678
uint32_t launcherElfUnits()
Space used by the launcher's ELF binary, in allocation units.
Definition: filesystem.h:673
A statically sized array.
Definition: array.h:51
void exec() const
Transfer control to a new program.
Definition: filesystem.h:289
uint32_t totalBytes()
Total space, in bytes.
Definition: filesystem.h:652
StoredObject(unsigned k)
Initialize from an unsigned integer.
Definition: filesystem.h:94
const char * version(const char *placeholder="(none)") const
Retrieve a mapped, NUL-terminated string with the volume's version string.
Definition: filesystem.h:514
bool operator!=(_SYSObjectKey other) const
Inequality comparison operator.
Definition: filesystem.h:221
Definition: array.h:34
#define NULL
Definition: macros.h:356
Volume type for the system launcher.
Definition: filesystem.h:245
Volume type for normal games.
Definition: filesystem.h:244
static const unsigned LIMIT
Maximum allowed value for a key (255)
Definition: filesystem.h:80
Information about the composition of the filesystem.
Definition: filesystem.h:611
Volume()
Default constructor, leaves the Volume with an invalid value of zero.
Definition: filesystem.h:249
Array< AssetGroup, MAX_BOOTSTRAP_GROUPS > BootstrapAssetGroups
An array large enough to hold all bototstrap AssetGroups.
Definition: filesystem.h:376
void * metadata(unsigned key, unsigned minSize, unsigned *actualSize) const
Look up a metadata value from the mapped Volume.
Definition: filesystem.h:440
uint32_t gameObjBytes()
Space used by all game StoredObjects, in bytes.
Definition: filesystem.h:708
uint32_t selfElfUnits()
Space used by the current volume's ELF binary, in allocation units.
Definition: filesystem.h:713
static const unsigned MAX_SIZE
Maximum size of an object, in bytes (4080)
Definition: filesystem.h:83
uint32_t systemBytes()
Space used by system data, in bytes.
Definition: filesystem.h:668
uint32_t gameElfBytes()
Space used by all game ELF binaries, in bytes.
Definition: filesystem.h:698
uint32_t gameObjUnits()
Space used by all game StoredObjects, in allocation units.
Definition: filesystem.h:703
A lightweight ID for a persistently stored object.
Definition: filesystem.h:75
StoredObject(int k)
Initialize from an integer.
Definition: filesystem.h:89
Type
Enumeration of common volume types.
Definition: filesystem.h:243
uint32_t translate(uint32_t va) const
A variant of translate() for pointers that have already been cast to uint32_t, such as pointers that ...
Definition: filesystem.h:482
bool operator==(_SYSVolumeHandle other) const
Equality comparison operator.
Definition: filesystem.h:320
Volume volume() const
Returns the Volume associated with this mapping.
Definition: filesystem.h:422
void attach(Volume vol)
Attach this MappedVolume to a new volume.
Definition: filesystem.h:413
void append(_SYSAssetSlot slot, AssetGroup &group, _SYSVolumeHandle volume=0)
Add an AssetGroup to this configuration.
Definition: loader.h:154
int write(const void *data, unsigned dataSize) const
Save a new version of an object.
Definition: filesystem.h:172
Vector2< T > vec(T x, T y)
Create a Vector2, from a set of (x,y) coordinates.
Definition: math.h:658
uint32_t launcherObjBytes()
Space used by the launcher's StoredObjects, in bytes.
Definition: filesystem.h:688