Skip to content

Commit 9eb090b

Browse files
authored
Merge pull request #251 from sshanks-kx/cquick
remove some dup info, add links to api guide
2 parents 607b72e + 5a72194 commit 9eb090b

File tree

1 file changed

+77
-82
lines changed

1 file changed

+77
-82
lines changed

docs/interfaces/c-client-for-q.md

Lines changed: 77 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -228,10 +228,10 @@ The following functions are provided to interface with the q memory manager.
228228

229229
purpose | function
230230
-----------------------------------------------|-----------
231-
Increment the object‘s reference count | `r1(K)`
232-
Decrement the object‘s reference count | `r0(K)`
233-
Free up memory allocated for the thread‘s pool | `m9()`
234-
Set whether interning symbols uses a lock | `setm(I)`
231+
Increment the object‘s reference count | [`r1(K)`](capiref.md#r1-increment-refcount)
232+
Decrement the object‘s reference count | [`r0(K)`](capiref.md#r0-decrement-refcount)
233+
Free up memory allocated for the thread‘s pool | [`m9()`](capiref.md#m9-release-memory)
234+
Set whether interning symbols uses a lock | [`setm(I)`](capiref.md#setm-toggle-symbol-lock)
235235

236236
A reference count indicates the usage of an object, allowing the same object to be used by more than one piece of code.
237237

@@ -288,7 +288,7 @@ and in q with
288288
-16!x
289289
```
290290

291-
The function `k`, as in
291+
The function [`k`](capiref.md#k-evaluate), as in
292292

293293
```c
294294
K r=k(handle,"functionname",params,(K)0);
@@ -317,22 +317,22 @@ To create atom values the following functions are available. Function `ka` creat
317317

318318
purpose | call
319319
-----------------------|-----------------
320-
Create an atom of type | `K ka(I);`
321-
Create a boolean | `K kb(I);`
322-
Create a guid | `K ku(U);`
323-
Create a byte | `K kg(I);`
324-
Create a short | `K kh(I);`
325-
Create an int | `K ki(I);`
326-
Create a long | `K kj(J);`
327-
Create a real | `K ke(F);`
328-
Create a float | `K kf(F);`
329-
Create a char | `K kc(I);`
330-
Create a symbol | `K ks(S);`
331-
Create a timestamp | `K ktj(-KP,J);`
332-
Create a time | `K kt(I);`
333-
Create a date | `K kd(I);`
334-
Create a timespan | `K ktj(-KN,J);`
335-
Create a datetime | `K kz(F);`
320+
Create an atom of type | [`K ka(I);`](capiref.md#ka-create-atom)
321+
Create a boolean | [`K kb(I);`](capiref.md#kb-create-boolean)
322+
Create a guid | [`K ku(U);`](capiref.md#ku-create-guid)
323+
Create a byte | [`K kg(I);`](capiref.md#kg-create-byte)
324+
Create a short | [`K kh(I);`](capiref.md#kh-create-short)
325+
Create an int | [`K ki(I);`](capiref.md#ki-create-int)
326+
Create a long | [`K kj(J);`](capiref.md#kj-create-long)
327+
Create a real | [`K ke(F);`](capiref.md#ke-create-real)
328+
Create a float | [`K kf(F);`](capiref.md#kf-create-float)
329+
Create a char | [`K kc(I);`](capiref.md#kc-create-char)
330+
Create a symbol | [`K ks(S);`](capiref.md#ks-create-symbol)
331+
Create a timestamp | [`K ktj(-KP,J);`](capiref.md#ktj-create-timestamp)
332+
Create a time | [`K kt(I);`](capiref.md#kt-create-time)
333+
Create a date | [`K kd(I);`](capiref.md#kd-create-date)
334+
Create a timespan | [`K ktj(-KN,J);`](capiref.md#ktj-create-timespan)
335+
Create a datetime | [`K kz(F);`](capiref.md#kz-create-datetime)
336336

337337
An example of creating an atom:
338338

@@ -352,8 +352,8 @@ K z = ki(42);
352352

353353
To create
354354

355-
- a simple list `K ktn(I type,J length);`
356-
- a mixed list `K knk(I n,...);`
355+
- a simple list [`K ktn(I type,J length);`](capiref.md#ktn-create-vector)
356+
- a mixed list [`K knk(I n,...);`](capiref.md#knk-create-list)
357357

358358
where `length` is a non-negative, non-null integer.
359359

@@ -368,10 +368,10 @@ As we've noted, the type of a mixed list is 0, and the elements are pointers to
368368

369369
To join
370370

371-
- an atom to a list: `K ja(K*,V*);`
372-
- a string to a list: `K js(K*,S);`
373-
- another K object to a list: `K jk(K*,K);`
374-
- another K list to the first: `K jv(K*,K);`
371+
- an atom to a list: [`K ja(K*,V*);`](capiref.md#ja-join-value)
372+
- a string to a list: [`K js(K*,S);`](capiref.md#js-join-string)
373+
- another K object to a list: [`K jk(K*,K);`](capiref.md#jk-join-k-object)
374+
- another K list to the first: [`K jv(K*,K);`](capiref.md#jv-join-k-lists)
375375

376376
The join functions assume there are no other references to the list, as the list may need to be reallocated during the call.
377377
In case of reallocation passed `K*` pointer will be updated to refer to new K object and returned from the function.
@@ -403,12 +403,12 @@ Strings and datetimes are special cases and extra utility functions are provided
403403
404404
purpose | function
405405
-----------------------------------|--------------------------
406-
Create a char array from string | `K kp(string);`
407-
Create a char array from string of length n | `K kpn(string, n);`
408-
Intern a string | `S ss(string);`
409-
Intern n chars from a string | `S sn(string,n);`
410-
Convert q date to yyyymmdd integer | `I dj(date);`
411-
Encode a year/month/day as q date <br/>`0==ymd(2000,1,1)`| `I ymd(year,month,day);`
406+
Create a char array from string | [`K kp(string);`](capiref.md#kp-create-string)
407+
Create a char array from string of length n | [`K kpn(string, n);`](capiref.md#kpn-create-fixed-length-string)
408+
Intern a string | [`S ss(string);`](capiref.md#ss-intern-string)
409+
Intern n chars from a string | [`S sn(string,n);`](capiref.md#sn-intern-chars)
410+
Convert q date to yyyymmdd integer | [`I dj(date);`](capiref.md#dj-date-to-number)
411+
Encode a year/month/day as q date <br/>`0==ymd(2000,1,1)`| [`I ymd(year,month,day);`](capiref.md#ymd-numbers-to-date)
412412
413413
Recall that Unix time is the number of seconds since `1970.01.01D00:00:00` while q time types have an epoch of `2000.01.01D00:00:00`.
414414
@@ -458,9 +458,10 @@ kS(v)[i] = ss("some symbol");
458458

459459
To create
460460

461-
- a dict: `K xD(K,K);`
462-
- a table from a dict: `K xT(K);`
463-
- a simple table from a keyed table: `K ktd(K);`
461+
- a dict: [`K xD(K,K);`](capiref.md#xd-create-dictionary)
462+
- a table from a dict: [`K xT(K);`](capiref.md#xt-table-from-dictionary)
463+
- a simple table from a keyed table: [`K ktd(K);`](capiref.md#ktd-create-simple-table)
464+
- a keyed table: [`K knt(J,K);`](capiref.md#knt-create-keyed-table)
464465

465466
A dictionary is a K object of type 99. It contains a list of two K objects; the keys and the values. We can use `kK(x)[0]` and `kK(x)[1]` to get these contained data.
466467

@@ -473,12 +474,12 @@ The following example shows the steps to create a keyed table:
473474
```c
474475
K maketable(){
475476
K c,d,e,v,key,val;
476-
/* table of primary keys */
477+
/* table of primary keys */
477478
c=ktn(KS,1);kS(c)[0]=ss("sid");
478479
d=ktn(KS,3);kS(d)[0]=ss("ibm");kS(d)[1]=ss("gte");kS(d)[2]=ss("kvm");
479480
v=knk(1,d);
480481
key=xT(xD(c,v));
481-
/* table of values */
482+
/* table of values */
482483
c=ktn(KS,2);kS(c)[0]=ss("amt");kS(c)[1]=ss("date");
483484
d=ktn(KI,3);kI(d)[0]=100;kI(d)[1]=300;kI(d)[2]=200;
484485
e=ktn(KD,3);kI(e)[0]=2;kI(e)[1]=3;kI(e)[2]=5;
@@ -510,13 +511,13 @@ else
510511
511512
## Connecting to a q server
512513
513-
We use the `int khpu(host, port,username)` function to connect to a q server.
514+
We use the [`int khpu(host, port,username)`](capiref.md#khpu-connect-no-timeout) function to connect to a q server.
514515
Note you _must_ call `khpu` before generating any q data, and the very first call to `khpu` must not be concurrent to other `khpu` calls.
515516
To initialize memory without making a connection, use `khp("",-1);`
516517
517518
It is highly recommended to use `khpu` and supply a meaningful username, as this will help server administrators identify a user’s connection.
518519
519-
The `khp`,`khpu` and `khpun` functions are for use in stand-alone applications only; they are not for use within a q server via a shared library. Hence, to avoid potential confusion, these functions have been removed from more recent releases of q.
520+
The `khp`,`khpu`, `khpun` and `khpunc` functions are for use in stand-alone applications only; they are not for use within a q server via a shared library. Hence, to avoid potential confusion, these functions have been removed from more recent releases of q.
520521
521522
A timeout can be specified with function `khpun`.
522523
@@ -533,9 +534,9 @@ Return values for `khp`/`khpu`/`khpun` are:
533534
-2 - timeout(khpun case)
534535
```
535536

536-
Note that with the release of `c.o` with V2.6, `c.o` now tracks the connection type (pre-V2.6, or V2.6+). Hence to close the connection you must call `kclose` (instead of `close` or `closeSocket`) – this will clean up the connection tracking and close the socket.
537+
Note that with the release of `c.o` with V2.6, `c.o` now tracks the connection type (pre-V2.6, or V2.6+). Hence to close the connection you must call [`kclose`](capiref.md#kclose-disconnect) (instead of `close` or `closeSocket`) – this will clean up the connection tracking and close the socket.
537538

538-
The `k` function is used to send messages over the connection. If a positive handle is used then the call is synchronous, otherwise it is an asynchronous call.
539+
The [`k`](capiref.md#k-evaluate) function is used to send messages over the connection. If a positive handle is used then the call is synchronous, otherwise it is an asynchronous call.
539540

540541
```c
541542
// Connect to a q server on the localhost port 1234.
@@ -551,7 +552,7 @@ Note that the object returned from an async set call must not be passed to `r0`.
551552
There is no timeout argument for the `k(handle,…,(K)0)` call, but you can use socket timeouts as described below.
552553
553554
554-
## Unix domain sockets
555+
### Unix domain sockets
555556
556557
A Unix domain socket may be requested via the IP address `0.0.0.0`, e.g.
557558
@@ -560,7 +561,7 @@ int handle=khpu("0.0.0.0",5000,"user:password");
560561
```
561562

562563

563-
## SSL/TLS
564+
### SSL/TLS
564565

565566
To use this feature, you must link with one of [the `e` libs](#two-sets-of-files).
566567

@@ -576,7 +577,11 @@ There’s an additional return value for TLS connections, `-3`, which indicates
576577
577578
```c
578579
extern K sslInfo(K x); // returns an error if init fails, or a dict of settings similar to -26!x
579-
if(handle==-3){K x=ee(sslInfo((K)0));printf("Init error %s\n",xt==-128?x->s:"unknown");r0(x);}
580+
if(handle==-3){
581+
K x=ee(sslInfo((K)0));
582+
printf("Init error %s\n",xt==-128?x->s:"unknown");
583+
r0(x);
584+
}
580585
```
581586

582587
Prior to 4.1t 2023.11.10, SSL/TLS connections can be used from the initialization thread only, i.e. the thread which first calls any `khp` function since the start of the application. It can now be used for one-shot synchronous requests.
@@ -590,7 +595,7 @@ int h=khpunc("",-1,"",0,2); // remember to test the return value for -3
590595
```
591596

592597

593-
## Socket timeouts
598+
### Socket timeouts
594599

595600
There are a number of reasons not to specify or implement timeouts.
596601
Typically these will be hit at the least convenient of times when under load from e.g. a sudden increase in trading volumes.
@@ -617,7 +622,6 @@ if(c>0) sst(c,30000,45000); // timeout sends with 30s, receives with 45s
617622
```
618623

619624

620-
621625
## Bulk transfers
622626

623627
A kdb+tick feed handler can send one record at a time, like this
@@ -655,8 +659,8 @@ This example assumes rows with three fields: symbol, price and size.
655659

656660
Note the two different directions of error flow below.
657661

658-
1. To signal an error from your C code to kdb+ use the function `krr(S)`.
659-
A utility function `orr(S)` can be used to signal system errors.
662+
1. To signal an error from your C code to kdb+ use the function [`krr(S)`](capiref.md#krr-signal-c-error).
663+
A utility function [`orr(S)`](capiref.md#orr-signal-system-error) can be used to signal system errors.
660664
It is similar to `krr(S)`, but it appends a system error message to the user-provided string before passing it to `krr`.
661665

662666
1. To catch an error code from the results of a call to `r=k(h, …)`, check the return value and type.
@@ -672,9 +676,6 @@ if(r && -128==r->t)
672676
Under some network-error scenarios, `errno` can be used to obtain the details of the error,
673677
e.g. `perror(“network”);`
674678
675-
:fontawesome-regular-hand-point-right:
676-
[`krr`](capiref.md#krr-signal-c-error)
677-
678679
679680
## Return values
680681
@@ -702,7 +703,7 @@ K identity(){
702703

703704
## Callbacks
704705

705-
The `void sd0(I)` and `K sd1(I, K(*)(I))` functions are for use with callbacks and are available only within q itself, i.e. used from a shared library loaded into q.
706+
The [`void sd0(I)`](capiref.md#sd0-remove-callback) and [`K sd1(I, K(*)(I))`](capiref.md#sd1-set-function-on-loop) functions are for use with callbacks and are available only within q itself, i.e. used from a shared library loaded into q.
706707
The value of the file descriptor passed to `sd1` must be 0 &lt; `fd` &lt; 1024, and 1021 happens to be the maximum number of supported connections (recalling 0, 1, 2 used for stdin,stdout,stderr).
707708

708709
```c
@@ -717,7 +718,7 @@ sd0(d);
717718
sd0x(d,1);
718719
```
719720

720-
Each of the above calls removes the callback on `d` and calls `kclose(d)`. `sd0x(I d,I f)` was introduced in V3.0 2013.04.04: its second argument indicates whether to call `kclose(d)`.
721+
Each of the above calls removes the callback on `d` and calls `kclose(d)`. [`sd0x(I d,I f)`](capiref.md#sd0x-remove-callback-conditional) was introduced in V3.0 2013.04.04: its second argument indicates whether to call `kclose(d)`.
721722

722723
On Linux, `eventfd` can be used with `sd1` and `sd0`. Given a file efd.c
723724

@@ -766,32 +767,13 @@ Callbacks from `sd1` are executed on the main thread of q, in the handle context
766767

767768
## Serialization and deserialization
768769

769-
The `K b9(I,K)` and `K d9(K)` functions serialize and deserialize K objects.
770-
771-
```c
772-
b9(mode,kObject);
773-
```
770+
The [`K b9(I,K)`](capiref.md#b9-serialize) and [`K d9(K)`](capiref.md#d9-deserialize) functions serialize and deserialize K objects.
774771

775-
will generate a K byte vector that contains the serialized data for `kObject`.
772+
`b9` will generate a K byte vector that contains the serialized data.
776773
Since V3.0, for shared libraries loaded into q the value for `mode` must be -1.
777-
For standalone applications binding with c.o/c.dll, or shared libraries prior to V3.0, the values for `mode` can be
778-
779-
value | effect
780-
------|------
781-
-1 | valid for V3.0+ for serializing/deserializing within the same process
782-
0 | unenumerate, block serialization of timespan and timestamp (For working with versions prior to 2.6).
783-
1 | retain enumerations, allow serialization of timespan and timestamp. (Useful for passing data between threads).
784-
2 | unenumerate, allow serialization of timespan and timestamp
785-
3 | unenumerate, compress, allow serialization of timespan and timestamp
786-
4 | (reserved)
787-
5 | allow 1TB msgs, but no single vector may exceed 2 billion items
788-
6 | allow 1TB msgs, and individual vectors may exceed 2 billion items
774+
For standalone applications binding with c.o/c.dll, or shared libraries prior to V3.0, the values for `mode` can be viewed [here](capiref.md#b9-serialize).
789775

790-
```c
791-
d9(kObject);
792-
```
793-
794-
will deserialize the byte stream in `kObject` returning a new `kObject`.
776+
`d9` will deserialize the provided byte stream returning a new `kObject`.
795777
The byte stream passed to `d9` is not altered in any way.
796778
If you are concerned that the byte vector that you wish to deserialize may be corrupted, call `okx` to verify it is well formed first.
797779

@@ -811,29 +793,42 @@ else
811793
812794
## Miscellaneous
813795
814-
The `K dot(K x, K y)` function is the same as the q function `.[x;y]`.
796+
The [`K dot(K x, K y)`](capiref.md#dot-apply) function is the same as the q function `.[x;y]`.
815797
816798
```q
817799
q).[{x+y};(1 2;3 4)]
818800
4 6
819801
```
820802

821-
The dynamic link, `K dl(V* f, I n)`, function takes a C function that would take _n_ K objects as arguments and return a new K object, and returns a q function.
803+
The dynamic link, [`K dl(V* f, I n)`](capiref.md#dl-dynamic-link), function takes a C function that would take _n_ K objects as arguments and return a new K object, and returns a q function.
822804
It is useful, for example, to expose more than one function from an extension module.
823805

824806
```c
825807
#include "k.h"
826808
Z K1(f1){R r1(x);}
827809
Z K2(f2){R r1(y);}
828-
K1(lib){K y=ktn(0,2);x=ktn(KS,2);xS[0]=ss("f1");xS[1]=ss("f2");
829-
kK(y)[0]=dl(f1,1);kK(y)[1]=dl(f2,2);R xD(x,y);}
810+
K1(lib){
811+
K y=ktn(0,2);
812+
x=ktn(KS,2);
813+
xS[0]=ss("f1");
814+
xS[1]=ss("f2");
815+
kK(y)[0]=dl(f1,1);
816+
kK(y)[1]=dl(f2,2);
817+
R xD(x,y);
818+
}
830819
```
831820
832821
Alternatively, for simpler editing of your lib API:
833822
834823
```c
835824
#define sdl(f,n) (js(&x,ss(#f)),jk(&y,dl(f,n)))
836-
K1(lib){K y=ktn(0,0);x=ktn(KS,0);sdl(f1,1);sdl(f2,2);R xD(x,y);}
825+
K1(lib){
826+
K y=ktn(0,0);
827+
x=ktn(KS,0);
828+
sdl(f1,1);
829+
sdl(f2,2);
830+
R xD(x,y);
831+
}
837832
```
838833

839834
With the above compiled into `lib.so`:

0 commit comments

Comments
 (0)