Class: SimpleMmap::MappedFile

Inherits:
Object
  • Object
show all
Defined in:
ext/simple_mmap/mapped_file.c

Defined Under Namespace

Classes: MmapData

Instance Method Summary collapse

Instance Method Details

#closeObject

munmap() the current mmapped file



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'ext/simple_mmap/mapped_file.c', line 94

static VALUE sm_mapped_file_close(VALUE vself)
{
  VALUE vsm_map;
  simple_mmap_map *sm_map;
  
  sm_map = ALLOC(simple_mmap_map);
  vsm_map = rb_ivar_get(vself, rb_intern("@mmap_data"));
  Data_Get_Struct(vsm_map, simple_mmap_map, sm_map);
  
  if (sm_map->map != (caddr_t) -1)
    munmap(sm_map->map, sm_map->len);
  if (sm_map->fd != -1)
    close(sm_map->fd);
  
  return Qtrue;
}

#read_window_data(offset, length) ⇒ Object

Read length bytes starting at offset



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'ext/simple_mmap/mapped_file.c', line 117

static VALUE sm_mapped_file_read_window_data(VALUE vself, VALUE voffset, VALUE vlength) 
{
  size_t offset = NUM2INT(voffset);
  size_t length = NUM2INT(vlength);
  char buff[length];
  VALUE vsm_map;
  simple_mmap_map *sm_map;
  
  sm_map = ALLOC(simple_mmap_map);
  vsm_map = rb_ivar_get(vself, rb_intern("@mmap_data"));
  Data_Get_Struct(vsm_map, simple_mmap_map, sm_map);
  
  if (offset > sm_map->len) {
    return Qnil;
  }
  
  size_t curr = offset;
  size_t i;
  for(i = 0; i < length; ++i) {
    //printf("i=%i offset=%i length=%i curr=%i map->len=%i\n", i, offset, length, curr, sm_map->len);
    if ((offset + i) > sm_map->len)
      break;
    buff[i] = sm_map->map[curr++];
  }
  
  // If the range overflows, return part that overlaps
  if ((offset + length) > sm_map->len)
    return rb_str_new(buff, sm_map->len - offset);
      
  return rb_str_new(buff, length);
}

#sizeObject

Return size of mapped file



156
157
158
159
160
161
162
163
164
165
# File 'ext/simple_mmap/mapped_file.c', line 156

static VALUE sm_mapped_file_size(VALUE vself)
{
  VALUE vsm_map;
  simple_mmap_map *sm_map;

  sm_map = ALLOC(simple_mmap_map);
  vsm_map = rb_ivar_get(vself, rb_intern("@mmap_data"));
  Data_Get_Struct(vsm_map, simple_mmap_map, sm_map);
  return UINT2NUM(sm_map->len);
}