e576bb2002-10-11Martin Nilsson /* || This file is part of Pike. For copyright information see COPYRIGHT. || Pike is distributed under GPL, LGPL and MPL. See the file COPYING || for more information. */
1b10db2002-10-08Martin Nilsson 
dd3c382002-05-29Per Hedbor #include "global.h" #include "bignum.h" #include "object.h" #include "interpret.h" #include <shuffler.h>
90ac0c2002-05-29Per Hedbor /* Source: System.Memory * Argument: An initialized instance of the System.Memory class */
76f5062002-05-30Henrik Grubbström (Grubba) static struct program *shm_program = NULL;
dd3c382002-05-29Per Hedbor  struct sm_source { struct source s; struct object *obj; struct {
1588532012-12-29Jonas Walldén  char *data;
dd3c382002-05-29Per Hedbor  size_t len; } *mem; int offset, len; };
feb6822009-08-13Henrik Grubbström (Grubba) static struct data get_data( struct source *src, off_t len )
dd3c382002-05-29Per Hedbor {
feb6822009-08-13Henrik Grubbström (Grubba)  struct sm_source *s = (struct sm_source *)src;
dd3c382002-05-29Per Hedbor  struct data res;
13670c2015-05-25Martin Nilsson 
dd3c382002-05-29Per Hedbor  res.do_free = 0; res.off = 0; res.data = s->mem->data + s->offset;
13670c2015-05-25Martin Nilsson 
dd3c382002-05-29Per Hedbor  if( len > s->len ) { len = s->len; s->s.eof = 1; /* next read will be done from the next source */ } res.len = len; s->len -= len; s->offset += len; return res; }
feb6822009-08-13Henrik Grubbström (Grubba) static void free_source( struct source *src )
dd3c382002-05-29Per Hedbor {
feb6822009-08-13Henrik Grubbström (Grubba)  free_object(((struct sm_source *)src)->obj);
dd3c382002-05-29Per Hedbor } struct source *source_system_memory_make( struct svalue *s, INT64 start, INT64 len ) { struct sm_source *res;
017b572011-10-28Henrik Grubbström (Grubba)  if( TYPEOF(*s) != PIKE_T_OBJECT )
dd3c382002-05-29Per Hedbor  return 0;
76f5062002-05-30Henrik Grubbström (Grubba)  if (!shm_program) {
5e9fc02015-08-18Per Hedbor  push_static_text("System.Memory");
ad77142003-12-13Martin Nilsson  SAFE_APPLY_MASTER("resolv", 1);
76f5062002-05-30Henrik Grubbström (Grubba)  shm_program = program_from_svalue(Pike_sp - 1); if (!shm_program) { pop_stack(); return 0; } add_ref(shm_program); pop_stack(); }
9c14f32014-04-27Martin Nilsson  res = calloc( 1, sizeof( struct sm_source ) ); if( !res ) return NULL;
dd3c382002-05-29Per Hedbor 
13b5ed2014-05-26Per Hedbor  if( !(res->mem = get_storage( s->u.object, shm_program ) ) )
dd3c382002-05-29Per Hedbor  { free(res); return 0; }
90ac0c2002-05-29Per Hedbor  if( !res->mem->data || !res->mem->len ) { free(res); return 0; }
13670c2015-05-25Martin Nilsson 
3387322002-05-29Per Hedbor  res->s.free_source = free_source;
dd3c382002-05-29Per Hedbor  res->s.get_data = get_data; res->obj = s->u.object;
50ea682003-03-14Henrik Grubbström (Grubba)  add_ref(res->obj);
dd3c382002-05-29Per Hedbor  res->offset = start; if( len != -1 )
2d76f22005-05-20Martin Stjernholm  if( len > (ptrdiff_t) res->mem->len - start )
dd3c382002-05-29Per Hedbor  {
50ea682003-03-14Henrik Grubbström (Grubba)  sub_ref(res->obj);
dd3c382002-05-29Per Hedbor  free(res); return 0; } else res->len = len; else res->len = len; if( res->len <= 0 ) {
50ea682003-03-14Henrik Grubbström (Grubba)  sub_ref(res->obj);
dd3c382002-05-29Per Hedbor  free(res); return 0; } return (struct source *)res; } void source_system_memory_exit( ) {
76f5062002-05-30Henrik Grubbström (Grubba)  if (shm_program) { free_program( shm_program ); }
dd3c382002-05-29Per Hedbor } void source_system_memory_init( ) { }