Adding my own BIF

原文地址: http://www.trapexit.org/Adding_my_own_BIF


Adding my own BIF
From Erlang Community
caveat

unless you really know what you're doing, you'll be better off using a linked-in driver or a port.
steps

    1. run configure
    2. add your bifs to erts/emulator/beam/bif.tab

bif re:grep/2 bif re:compile/1

    3. create a C file

erts/emulator/beam/erl_bif_re.c

    4. add your C file to erts/emulator/<arch>/Makefile

RUN_OBJS = $(OBJDIR)/erl_bif_re.o \

    5. implement your bifs by stealing bits from existing erl_bif_*.c files

BIF_RETTYPE re_grep_2(BIF_ALIST_2){
  Eterm result;
  result = magic_function();
  BIF_RET(result);
}

    6. run make; make install

notes

    * steps 0-3 need only be done once.

    * note that if you add

bif re:grep/2

to bif.tab there should be a erl_bif_re.c that implements

BIF_RETTYPE re_grep_2(BIF_ALIST_2);

为什么要用bif呢? bif比驱动或者port的好处是 bif支持trap, 所以能够让cpu计算平均在各个进程分配,这个网络程序很重要的一个要求. 没有这个特性一个费时的操作会把整个调度器拖死,其他的进程就谈不上什么响应了。下篇文章教你如何写带trap功能的bif。

你可能感兴趣的:(C++,c,erlang,C#)