Hacker News new | past | comments | ask | show | jobs | submit login

Rather than creating new geometric primitives on the heap, wouldn't it be more flexible if the caller can provide the memory to initialize the structure in?

Then instead of

  struct tg_geom *geom = tg_geom_new_point(-112, 33);
  if (!geom) {
      // System is out of memory.
  }
you could do

  struct tg_geom geom;
  tg_geom_init_point(&geom, -112, 33);
without need for error checking or cleanup. You can still allocate on the heap if you want but you wouldn't have to.



I have strict safety rules for this project, one of which requires that all geometries are thread-safe pointers. So in the example you show, it would not be safe to share the tg_geom pointer with other threads because it belongs on the stack instead of the heap.

Yet I do see the need for avoiding allocations when working with lots of simple geometries like points and rects, and for my use I made functions like tg_geom_intersects_xy and tg_geom_intersects_rect to perform relation operations directly on point and rectangle values.


I think that this would be incompatible with the fast cloning/reference counting feature.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: