Class: MyMalloc

Inherits:
Object
  • Object
show all
Defined in:
lib/my_malloc.rb

Constant Summary collapse

VERSION =
"1.0"

Instance Method Summary collapse

Constructor Details

#initialize(size) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'ext/my_malloc/my_malloc.c', line 29

static VALUE
my_malloc_init(VALUE self, VALUE size) {
  struct my_malloc *ptr;
  size_t requested = NUM2SIZET(size);

  if (0 == requested)
    rb_raise(rb_eArgError, "unable to allocate 0 bytes");

  Data_Get_Struct(self, struct my_malloc, ptr);

  ptr->ptr = malloc(requested);

  if (NULL == ptr->ptr)
    rb_raise(rb_eNoMemError, "unable to allocate %ld bytes", requested);

  ptr->size = requested;

  return self;
}

Instance Method Details

#freeObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'ext/my_malloc/my_malloc.c', line 49

static VALUE
my_malloc_release(VALUE self) {
  struct my_malloc *ptr;

  Data_Get_Struct(self, struct my_malloc, ptr);

  if (0 == ptr->size)
    return self;

  ptr->size = 0;
  free(ptr->ptr);

  return self;
}