Creating extensions to the Pike language is a fairly simple task. Extensions can be written in C or C++, and may be hand coded or written using a preprocessed version of C called CMOD. CMOD simplifies the writing of C language modules by providing a number of simplifications, such as automatic handling of method and class registrations, argument handling and so forth. There are a number of limitations to the CMOD preprocessor, but for the majority of extensions, CMOD will work just fine.

A number of (as yet incomplete) documents attempt to describe the API for writing modules:

You can have INIT, EXIT and PIKEFUN blocks outside of a PIKECLASS directive. Because modules are already instantiated as objects, this allows your module to have functions as well as classes.

Working with references

Pike utilizes garbage collection and reference counting to handle memory management of pike data objects (strings, arrays, objects, etc). When writing C/CMOD code, you need to tell pike that you explicitly want to keep data around once it passes out of your function, along with the corresponding process of telling pike that you're done with the object afterward. Pike provides methods for doing this (though some methods will add references automatically).

add_ref(svalue val) is used to add a reference to a variable.

free_string(pike_string val) and its corresponding friends is used to subtract references and will free the variable if there are no more references to that variable. The name can be confusing but it's best to think of it from the standpoint of your code not knowing whether other code is using your variable. You're just saying that "it's ok by me if you want to free this value." Pike will actually do that when everone who said they wanted to keep it no longer want the value.

Looping through Mappings

The NEW_MAPPING_LOOP macro allows you to loop through all elements in a mapping. Note that the keypair name (k) is required and is hard coded.