/* *breif:添加一个新的条目.重新调整表大小. *param[cookie]: *param[obj]:要添加的条目. */ IndirectRef IndirectRefTable::add(u4 cookie, Object* obj) { IRTSegmentState prevState; prevState.all = cookie; size_t topIndex = segmentState.parts.topIndex; assert(obj != NULL); assert(dvmIsHeapAddress(obj)); assert(table_ != NULL); assert(alloc_entries_ <= max_entries_); assert(segmentState.parts.numHoles >= prevState.parts.numHoles); /* * We know there's enough room in the table. Now we just need to find * the right spot. If there's a hole, find it and fill it; otherwise, * add to the end of the list. */ IndirectRef result; IndirectRefSlot* slot; int numHoles = segmentState.parts.numHoles - prevState.parts.numHoles; if (numHoles > 0) { assert(topIndex > 1); /* find the first hole; likely to be near the end of the list, * we know the item at the topIndex is not a hole */ slot = &table_[topIndex - 1]; assert(slot->obj != NULL); while ((--slot)->obj != NULL) { assert(slot >= table_ + prevState.parts.topIndex); } segmentState.parts.numHoles--; } else { /* add to the end, grow if needed */ if (topIndex == alloc_entries_) { /* reached end of allocated space; did we hit buffer max? */ if (topIndex == max_entries_) { ALOGE("JNI ERROR (app bug): %s reference table overflow (max=%d)", indirectRefKindToString(kind_), max_entries_); return NULL; } size_t newSize = alloc_entries_ * 2; if (newSize > max_entries_) { newSize = max_entries_; } assert(newSize > alloc_entries_); IndirectRefSlot* newTable = (IndirectRefSlot*) realloc(table_, newSize * sizeof(IndirectRefSlot)); if (table_ == NULL) { ALOGE("JNI ERROR (app bug): unable to expand %s reference table " "(from %d to %d, max=%d)", indirectRefKindToString(kind_), alloc_entries_, newSize, max_entries_); return NULL; } memset(newTable + alloc_entries_, 0xd1, (newSize - alloc_entries_) * sizeof(IndirectRefSlot)); alloc_entries_ = newSize; table_ = newTable; } slot = &table_[topIndex++]; segmentState.parts.topIndex = topIndex; } slot->obj = obj; slot->serial = nextSerial(slot->serial); result = toIndirectRef(slot - table_, slot->serial, kind_); assert(result != NULL); return result; }
int main(int argc, char *argv[]) { int sockfd, newsockfd, portno, pid; #ifdef __LINUX__ socklen_t clilen; #else int clilen; #endif struct sockaddr_in serv_addr, cli_addr; if (argc < 2) { fprintf(stderr,"ERROR, no port provided\n"); exit(1); } sockfd = socket(AF_INET, SOCK_STREAM, 0); //create socket if (sockfd < 0) error("ERROR opening socket"); memset((char *) &serv_addr, 0, sizeof(serv_addr)); //reset memory //fill in address info portno = atoi(argv[1]); serv_addr.sin_family = AF_INET; serv_addr.sin_addr.s_addr = INADDR_ANY; serv_addr.sin_port = htons(portno); if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) error("ERROR on binding"); cleanCache(); while(1){ listen(sockfd,5); //5 simultaneous connection at most //accept connections newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen); if (newsockfd < 0) error("ERROR on accept"); int n; char buffer[BUFLEN]; memset(buffer, 0, BUFLEN); //reset memory invalidateOptionList(); do{ //read client's message n = recv(newsockfd,buffer,BUFLEN-1,0); if (n < 0) error("ERROR reading from socket"); printf("Here is the message: \n%s\n",buffer); /*Null terminate the buffer so we can use string operations on it.*/ buffer[n] = '\0'; /*process request*/ n = processRequest(buffer, newsockfd); /*process failed*/ if (n == -1) printf("Request was not successful."); printf("\n---------------------next request--------------------\n"); nextSerial(); if (auth >= 0) printf("Next Serial Number should be %d.\n-------------------\n",savedSerial); }while(auth >= 0); close(newsockfd);//close connection } close(sockfd); return 0; }